From: http://www.javaeye.com/topic/308966
Generally, we use the tofixed function provided by JS to perform rounding operations. However, when the value is very small, we find the problem.
For example:
- <Script language = "JavaScript">
- VaR money = 0.00542;
- Alert (number (money). tofixed (2 ));
- </SCRIPT>
The output result is 0.00, which is obviously incorrect.
The following method uses the math. Round Function to solve the hour-level precision problem.
For example:
- <Script language = "JavaScript">
- VaR money = 0.00542;
- // Alert (number (money). tofixed (2 ));
- Function round2 (number, fractiondigits ){
- With (math ){
- Return round (Number * POW (10, fractiondigits)/POW (10, fractiondigits );
- }
- }
- Alert (round2 (money, 2 ));
- </SCRIPT>
Output result: 0.01.
Note:
Round: 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.
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.