Enter two numbers in html to realize addition, subtraction, multiplication and division functions

Enter two numbers in html to realize addition, subtraction, multiplication and division functions

1. parseFloat() function

Make a simple calculator on a web page, enter two numbers in the text box, and be able to add, subtract, multiply and divide the two numbers.
The parseFloat() function parses a string and returns a floating point number.
This function specifies whether the first character in a string is a number. If it is, the string is parsed until the end of the number is reached and the number is returned as a number.
instead of as a string.
Syntax: parseFloat(string);

2. JavaScript global properties

property describe
Infinity A value representing positive infinity.
NaN Indicates whether a value is a numeric value.
undefined Indicates an undefined value.

3. Complete code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript">
       function cal(){
           var nums = document.getElementsByName("num");
           var sz = document.getElementsByName("js");
           var result = document.getElementsByName("rs");
           var n1 = parseFloat(nums[0].value);
           var n2 = parseFloat(nums[1].value);
           /*parseFloat() function parses a string and returns a floating point number.
           This function specifies whether the first character in a string is a number. If it is, the string is parsed until the end of the number is reached and the number is returned as a number.
           instead of as a string.
        */
            switch(sz[0].value){
            case "add" : 
            result[0].value = n1 + n2;
            break;
            case "min" : 
            result[0].value = n1 - n2;
            break;
            case "mul" : 
            result[0].value = n1 * n2;
            break;
            case "div" : 
            result[0].value = n1/n2; 
            break;
              }
          }
      </script>
</head>
<body>
    <div align="center">
        <input type="text" name="num" value="" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" />
        <select name="js" size="1">
            <option value="add">+</option>
            <option value="min">-</option>
            <option value="mul">*</option>
            <option value="div">/</option>
        </select>
   <input type="text" name="num" value=""onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" />
  =
   <input type="text" name="rs" value=""/><br>
   <button id="btn"onclick="cal()">Calculate</button>
</div>
</body>
</html>

Effect display:

insert image description here

This is the end of this article about how to add, subtract, multiply and divide two numbers by inputting them in html. For more relevant html addition, subtraction, multiplication and division content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Why is there this in JS?

>>:  Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations

Recommend

Use of LRU algorithm in Vue built-in component keep-alive

Table of contents The use of Vue's keep-alive...

Implementation steps for setting up the React+Ant Design development environment

Basics 1. Use scaffolding to create a project and...

Html comments Symbols for marking text comments in Html

HTML comments, we often need to make some HTML co...

Windows 2019 Activation Tutorial (Office2019)

A few days ago, I found that the official version...

How to configure SSL for koa2 service

I. Introduction 1: SSL Certificate My domain name...

Detailed example of database operation object model in Spring jdbc

Detailed example of database operation object mod...

Select does not support double click dbclick event

XML/HTML CodeCopy content to clipboard < div c...

Use CSS content attr to achieve mouse hover prompt (tooltip) effect

Why do we achieve this effect? ​​In fact, this ef...

About installing python3.8 image in docker

Docker Hub official website 1. Search for Python ...

How to delete the container created in Docker

How to delete the container created in Docker 1. ...

The difference between html, xhtml and xml

Development Trends: html (Hypertext Markup Languag...

js native waterfall flow plug-in production

This article shares the specific code of the js n...