Someone asked JavaScript how to intercept decimal places and rounding them out. This is a very common JS technology. Many new users may encounter this problem, so I sorted out the post, for new users.
1. Round Method
Purpose: return the nearest integer to the given numeric expression.
Syntax: Math. Round (number). The required number parameter is the value to be rounded to the nearest integer.
Note: 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.
2. In JavaScript 1.5 (supported by ie5.5 + and ns6 +), two functions dedicated to currency circulation are added, number. tofixed (X) and number. toprecision (X ).
Number. tofixed (x) truncates the specified number to X digits after the decimal point, and number. toprecision (x) truncates the entire number to a specified length (X. Note: one is to calculate the length after the decimal point, and the other is to calculate the length of the entire number.
For example:
<SCRIPT type = "text/JavaScript">
VaR AA = 2.3362;
Document. Write (AA. tofixed (1); // 2.3
Document. Write (AA. tofixed (2); // 2.34
Document. Write (AA. toprecision (2); // 2.3
Document. Write (AA. toprecision (3); // 2.34
Document. Write (math. Round (AA * 10)/10); // 2.3
Document. Write (math. Round (AA * 100)/100); // 2.34
</SCRIPT>
Because it is a new function, you need to consider browser support issues.