The rounded function toFixed(n) in native javascript, n is the number of decimal places to be retained. (0<= n <= 20)
The code copy is as follows:
var num=1.0999;
console.log(num.toFixed(20));
http://jsfiddle.net/14x0vhu6/
The output value is not the expected 1.0999, but 1.0999000000000000009983. This needs to be paid attention to, and the reasons need to be improved.
In addition, in different browser versions, if the decimal point and the previous digit to be intercepted are 0, it may cause interception without reason.
The code copy is as follows:
var num=0.07;
console.log(num.toFixed(1));
http://jsfiddle.net/ogwnw2j3/
The value may be 0.0
The method of processing is to add 1 before using the toFixed method and then subtract 1 after using it.
The code copy is as follows:
var number=0.07
var fixNum = new Number(number + 1).toFixed(1);//Add 1 before rounding
var fixedNum = new Number(fixNum - 1).toFixed(1);//Fixed after rounding, subtract 1, and round it up again
console.log(fixedNum);
http://jsfiddle.net/euvn0L1g/