Yesterday, when using JS to the foreground floating-point number to find that:
0.05+1.08=1.1300000000000001
Check on the Internet there is indeed such a bug, in addition to the number of digits on the control there is no good way (hope that experts can put forward
Other ideas).
So I write a control of the decimal number of the JS method to solve the development of the immediate
Copy Code code as follows:
Decimal digit control, can be rounded
function fractional (n) {
Number of decimal reserved digits
var bit = 2;
Add a decimal point to expand the 1-bit
bit++;
Number to String
n = n.tostring ();
Get decimal point position
var point = N.indexof ('. ');
The length of n is greater than the length of the reserved digits
if (N.length > point + Bit) {
The number of decimal digits is greater than 4, greater than 4
if (parseint (n.substring (point + bit, point + bit + 1)) > 4) {
Return n.substring (0, point) + "." + (parseint (n.substring (point + 1, point + bit)) + 1);
}
else {
Return n.substring (0, point) + n.substring (point, point + bit);
}
}
return n;
}