Today in the project to develop a calculation of the amount of functionality, I started to do this:
eg
var amount + = parsefloat (0.01+0.02);
Results: 0.0300000000000000001
Why is it?
Search the parsefloat theme:
1. The parsefloat () function---Parse string---returns a floating-point number.
During the parsing process, I encountered
plus sign (+ or-),
Numbers (0-9),
Decimal point
Index in science notation (e or E)
other than the characters ,
It ignores the character and all subsequent characters. Whitespace characters at the top of the parameter string are ignored.
The first character cannot be parsed into a number---"returns NaN.
Well..... Feeling or understanding is not deep, why would be so a long string, precision problems, see how the principle ....
But..... Didn't find a clear explanation of the underlying
Let's take a look at the solution for a moment:
(1)
ToFixed (n) But this method is not compatible with JS version requirements IE5
(2)
var price = 5.02
var quantity = 4
var amount= math.round (parsefloat (price*100 *quantity))/100;
Math.ceil () is rounding up
Math.floor () is rounded down
Math.Round () is rounded
Welcome to provide solutions, answer doubts ....
Summary: JavaScript amount calculation