Math.round () function returns a number rounded to the nearest integer.

grammar:

Math.round(x);

Parameters: x

Return Value: value of a given number rounded to the nearest integer

description:

If the fractional part of the argument is larger than 0.5, the absolute value is rounded to an adjacent integer greater. If the fractional part of the argument is less than 0.5, it is rounded to an adjacent integer smaller absolute value. If the parameter is exactly equal to the fractional part of 0.5, it is rounded to an adjacent integer in the positive infinity (+ ∞) direction. Note, many other languages ​​round () function is different, Math.round () is not always in the direction away from zero rounding (especially in the negative case exactly equal to the fractional part of 0.5).

Because the round () is Math static method, you should use direct Math.round () , rather than as you create a Math an instance of an object method to use ( Math is not a constructor).

Examples

x=Math.round(20.49);//20
x=Math.round(20.5); //21
x=Math.round(-20.5); //-20
x=Math.round(-20.51);//-21

 

Guess you like

Origin www.cnblogs.com/sitian2050/p/11824508.html