Javascript floating point number product operations appear in a number of decimal solutions

  • 2020-03-30 01:45:05
  • OfStack

Javascript is taking the product of floating point Numbers, and there is a multi-digit decimal situation.

This is due to the operation of converting floating point Numbers into binary Numbers before the operation, but some decimal Numbers appear infinite loop after the binary coding, resulting in errors in the calculation, and there are similar problems in other converted languages.

The explanation of the reason is referred to from baidu:

For example: find 1038.1-1000
1038.1 = 10000001110.0001100110011001100110011001100110011001100...
1000 = 1111101000

1038.1 to binary is an infinite repeating decimal, 1100 is a repeating node, it can only be approximated, the error is generated here if the browser version is high, you can use the toFixed() method to round the Number to the Number of decimal places.

Solution: multiply the product by (10^4), divide by (10^4), and approximate math.round based on the number of decimal places you want to preserve

 
var m1 = 2232.00, 
percent = (10/100), 
total = percent*m1; 
alert(total);//223.20000000000002 

total = Math.round(total*10)/10; 
alert(total);//223.2 


Related articles: