Home >Web Front-end >Front-end Q&A >Why does javascript decimal subtraction have a long list of decimal places?

Why does javascript decimal subtraction have a long list of decimal places?

青灯夜游
青灯夜游Original
2021-06-18 15:50:172593browse

Reason: The integer type will be automatically converted into an integer type for calculation, and the decimal type will be directly converted into a double type for calculation; and the double type needs to be accurate to 15 digits after the decimal point, so the JavaScript subtraction of decimals will result in a long string of decimal places.

Why does javascript decimal subtraction have a long list of decimal places?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The reason why a long string of decimal places appears when subtracting decimals in javascript

<script>
var a=&#39;38.8&#39;;
var b=&#39;6.8&#39;;
alert(parseFloat(a)-parseFloat(b));
var a=134.22;
var b=6;
alert(a*b);
</script>

Why does the above code produce a long string of decimal places? Although it is more accurate, it can It's not necessary.

This is related to the data structure. The integer type is automatically converted into a positive type for calculation, and the decimal type is directly converted into a double type for calculation. This is necessary when computing in memory. You should know that the computer only recognizes 0 and 1. The specific problem is the accuracy of floating point.

  • float is accurate to 7 decimal places

  • double is accurate to 15 decimal places

javascript:document.write( (11.3-10.1).toFixed(2) )
## The #toFixed() method not only truncates excess decimal places, it also rounds based on the next decimal place at the interception position. For example, for the value 10.739, truncated to two digits after the decimal point, the result would be 10.74. For the value 10.732, truncated to two digits after the decimal point, the result will be 10.73.

Note that in JavaScript we can only intercept decimals from 0 to 20 digits after the decimal point.

The toFixed() method is only supported by higher versions of browsers, so it is best to check whether the browser supports this method before using it. The check code is as follows:

var varNumber = 22.234;
 
if (varNumber.toFixed)
{
varNumber = varNumber.toFixed(2);
}
else //浏览器不支持toFixed()就使用其他方法
{
var div = Math.pow(10,2);
varNumber = Math.round(varNumber * div) / div;
}

This can be solved, but you want to ask how it is possible to have so many decimal points.

Why is there such an incomprehensible answer?

I Googled it and found out that this is a bug in JavaScript floating point operations.

For example: 7*0.8 JavaScript calculates it as: 5.6000000000000005

I found some solutions online, which is to rewrite some floating point operations functions.

These methods are excerpted below for reference by friends who encounter the same problem:

Program code

//除法函数,用来得到精确的除法结果
//说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
//调用:accDiv(arg1,arg2)
//返回值:arg1除以arg2的精确结果
 
function accDiv(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);
}
}
//给Number类型增加一个div方法,调用起来更加方便。
Number.prototype.div = function (arg){
return accDiv(this, arg);
}
//乘法函数,用来得到精确的乘法结果
//说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
//调用:accMul(arg1,arg2)
//返回值:arg1乘以arg2的精确结果
function accMul(arg1,arg2)
{
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}
//给Number类型增加一个mul方法,调用起来更加方便。
Number.prototype.mul = function (arg){
return accMul(arg, this);
}
//加法函数,用来得到精确的加法结果
//说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
//调用:accAdd(arg1,arg2)
//返回值:arg1加上arg2的精确结果
function accAdd(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
}
//给Number类型增加一个add方法,调用起来更加方便。
Number.prototype.add = function (arg){
return accAdd(arg,this);
}

Include these functions where you want to use them, and then Just call it to calculate.

For example, if you want to calculate: 7*0.8, change it to (7).mul(8)

Other operations are similar, and you can get more accurate results.

[Related recommendations:

javascript learning tutorial]

The above is the detailed content of Why does javascript decimal subtraction have a long list of decimal places?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn