Here are some ways to round decimal values to integers: Math.ceil (), Math.floor (), and Math.Round (). These three methods follow the following rounding rules, respectively:
Math.ceil () performs rounding up, that is, it always rounds the value up to the nearest integer;
Math.floor () performs rounding down, that is, it always rounds the value down to the nearest integer;
Math.Round () performs standard rounding, that is, it always rounds the value to the nearest integer (which is also the rounding rule we learned in math Class).
The following are examples of using these methods:
Alert (Math.ceil (25.9));
Alert (Math.ceil (25.5)),//26
alert (Math.ceil (25.1)),//26
alert (
math.round); 25.9 Alert (Math.Round (25.5));
Alert (Math.Round (25.1)),//25
alert (Math.floor (25.9)),//25
alert (
math.floor); 25.5 Alert (Math.floor (25.1)); 25
Nanchang Network Company Technician Summary: For all values between 25 and 26 (excluding 26), Math.ceil () always returns 26, because it performs rounding up. The Math.Round () method returns 26 only if the value is greater than or equal to 25.5, otherwise it returns 25. Finally, Math.floor () returns 25 for all values between 25 and 26 (excluding 26).
The following are some additions to
:
ceil (): The fractional part is rounded to the integer part.
such as:
Math.ceil (12.2)/return
Math.ceil (12.7)//return
Math.ceil (12.0)//Return to
Floor (): All go, just keep the whole Number.
such as:
Math.floor (12.2)//Return to
Math.floor (12.7)//Return to
Math.floor (12.0)//Return to
Round (): Rounding
such as:
Math.Round (12.2)//Return to
Math.Round (12.7)//return
Math.Round (12.0)//Return to