The round function can be rounded, it is a rounding function, the following is a look at the syntax of round:
1 Math.Round (number)
Let's look at a few examples:
The code is as follows |
|
1 document.write (Math.Round (2.65));//Print 3 2 document.write (Math.Round (7.05));//Print 7 3 document.write (Math.Round ( -2.65));//Print-3 4 document.write (Math.Round ( -8.15));//Print-8 5 document.write (Math.Round (11.65));//Print 12 |
The rounding of the round function is only for the first digit after the decimal point, and if you want to target the rest of the decimal point, we can multiply the number by 10, and then divide the round by the same number:
The code is as follows |
|
1 var my_val=11.257; 2 Var my_val=11.254; 3 document.write (Math.Round (my_val*100)/100); |
JavaScript Round () method
Definitions and usage
The round () method rounds a number to the nearest integer.
Grammar
Math.Round (x)
Parameters |
Description |
X |
Necessary. Must be a number. |
return value
The nearest integer to X.
Description
For 0.5, the method is rounded up.
For example, 3.5 will be rounded to 4, and-3.5 will be rounded to-3.
Instance
Rounds a different number to the nearest integer:
The code is as follows |
|
<script type= "Text/javascript" > document.write (Math.Round (0.60) + "<br/>") document.write (Math.Round (0.50) + "<br/>") document.write (Math.Round (0.49) + "<br/>") document.write (Math.Round ( -4.40) + "<br/>") document.write (Math.Round (-4.60)) </script> Output: 1 1 0 -4 -5 |
JavaScript Math.Round () function
Math.Round (x)--Returns the nearest integer to the number, rounded
Round, Chinese "rounding"
Round function syntax
Math.Round (x);
X--Numbers of type number
Round function return value
Returns the nearest integer for x, if the decimal part of X is greater than or equal to 0.5, the return value is the smallest integer greater than x, otherwise the round function returns the largest integer less than or equal to X
Round Function Example
The code is as follows |
|
document.write (Math.Round (5.8)); document.write (Math.Round (5.2)); document.write (Math.Round (-5.8)); document.write (Math.Round (-5.2)); Results: 6 5 -6 -5 |