A detailed introduction to the operation of JavaScript floating-point numbers

Source: Internet
Author: User
Tags numeric pow

Similarly, we can imagine that the 1/3 result should be 0.3333333333333333.
To get the results we want, we need to correct this value.


In JS, the parsefloat function can be used to convert strings into floating-point numbers. We can use the parseint function if we want to convert a string into an integer. You can only convert numeric characters to floating-point numbers by parsefloat, and you cannot convert alphabetic characters to floating-point numbers.

The main purpose of the parsefloat function is to convert the string entered by the user from the text box into a floating-point number.
Let's use an example to illustrate the usage of the parsefloat function, where we place two text boxes, let the user enter two digits, and then compute and display them in a third text box.

The code is as follows Copy Code


<title> Script Division </title>
<script type= "Text/javascript" >
function To_add () {
Document.f1.t3.value=parsefloat (Document.f1.t1.value) + parsefloat (document.f1.t2.value);
}
</script>
<body>
<form method=post name=f1 ><br>
Enter fist value<input type=text name=t1 onblur= "To_add ();" ><br>
Enter Second value<input type=text name=t2 onblur= "To_add ();" ><br>
Sum <input Type=text name=t3>
</form>
</body>

Note: The parsefloat function will try to help you convert strings into functions, such as

View Source code print?

The code is as follows Copy Code
1 document.write (parsefloat ("1234abc45"));

Output: 1234

That is, the parsefloat function converts numbers from the beginning of the string to the point where the non-numeric character stops, and "abc123", such that the string cannot be converted successfully.

These are not the results we want, let's look at a function to solve this problem

The code is as follows Copy Code

The

//division function, which is used to get the exact division result
//Description: The JavaScript division result is error, which is more noticeable when dividing two floating-point numbers. This function returns a more precise division result.   
//Call: Division (ARG1,ARG2)   
//return value: Arg1 divided by arg2 exact result
function Division (ARG1,ARG2) {
 var t1=0,t2=0,r1,r2;
 try{t1=arg1.tostring (). Split (".") [1].length}catch (e) {}
 try{t2=arg2.tostring (). Split (".") [1].length}catch (e) {}
 with (Math) {
  r1=number (arg1.tostring (). Replace (".", ""));
  r2=number (Arg2.tostring (). Replace (".", ""));
  return (R1/R2) *pow (10,T2-T1);
 }
}

multiplication function to get the exact result of the multiplication
Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
Call: Multiply (ARG1,ARG2)
return value: Arg1 times Arg2 's exact result
function Multiply (ARG1,ARG2) {
Arg1=string (arg1); var i=arg1.length-arg1.indexof (".") -1;i= (i>=arg1.length) 0:i;
Arg2=string (arg2); var j=arg2.length-arg2.indexof (".") -1;j= (j>=arg2.length) 0:j;
Return Arg1.replace (".", "") *arg2.replace (".", "")/math.pow (10,I+J);
}

The addition function, which is used to get the exact addition result.
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise addition result.
Call: Add (ARG1,ARG2)
return value: Arg1 plus arg2 's exact result
function Add (ARG1,ARG2) {
var r1,r2,m;
Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0}
Try{r2=arg2.tostring (). Split (".") [1].length}catch (e) {r2=0}
M=math.pow (10,math.max (R1,R2));
Return (arg1*m+arg2*m)/m;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.