Previously, I used the tofixed function provided by JS to perform rounding operations. However, I encountered problems in actual use.
For example
- VaRMoney = 0.00542;// 0.006;
- Alert (number (money). tofixed (2 ));
- // 0.00
We can see that the above results are incorrect. The following method uses the math. Round Function skillfully to completely solve the Accuracy Problem of a very small number.
VaRMoney = 0.00542;// 0.006;
Alert (number (money). tofixed (2 ));
FunctionRound2 (number, fractiondigits ){
With(Math ){
ReturnRound (Number * POW (10, fractiondigits)/POW (10, fractiondigits );
}
}
Alert (round2 (money, 2 ));// 0.01
Round Method
Returns the nearest integer to the given numeric expression.
Math. Round (number)
The required number parameter is the value to be rounded to the nearest integer.
Description
If the decimal part of number is greater than or equal to 0.5, the return value is the smallest integer greater than number. Otherwise, round returns the maximum integer less than or equal to number.