The functions rounded in native JavaScript are toFixed (n), and N is the number of decimal places to keep. (0<= n <=20)
Copy Code code as follows:
var num=1.0999;
Console.log (Num.tofixed (20));
http://jsfiddle.net/14x0vhu6/
The output value is not expected to be 1.0999, but 1.09990000000000009983, this needs attention, as for the reason to be perfected.
In a different browser version, if the decimal point and the previous one to intercept are 0 o'clock, there may be an irrational interception.
Copy Code code as follows:
var num=0.07;
Console.log (num.tofixed (1));
http://jsfiddle.net/ogwnw2j3/
The value may be 0.0
The approach is to add 1 before using the Tofixed method, and then subtract 1 after use.
Copy Code code as follows:
var number=0.07
var fixnum = new Number (number + 1). toFixed (1);/Before rounding plus 1
var fixednum = new Number (fixNum-1). toFixed (1);//After rounding minus 1, then rounding.
Console.log (Fixednum);
http://jsfiddle.net/euvn0L1g/