JavaScript Rounding Methods of the Math object

    The next group of methods has to do with rounding decimal values into integers. Three methods — Math.ceil(),  Math.floor(), and  Math.round() — handle rounding in different ways as
described here:
    The  Math.ceil() method represents the ceiling function, which always rounds numbers up
to the nearest integer value.
    The  Math.floor() method represents the fl  oor function, which always rounds numbers
down to the nearest integer value.

    The  Math.round() method represents a standard round function, which rounds up if the
number is at least halfway to the next integer value (0.5 or higher) and rounds down if not.
This is the way you were taught to round in elementary school.

    The following example illustrates how these methods work:

alert(Math.ceil(25.9));     //26
alert(Math.ceil(25.5));     //26
alert(Math.ceil(25.1));     //26

                   
alert(Math.round(25.9));    //26
alert(Math.round(25.5));    //26
alert(Math.round(25.1));    //25

        
alert(Math.floor(25.9));    //25
alert(Math.floor(25.5));    //25
alert(Math.floor(25.1));    //25

 

 

 

你可能感兴趣的:(JavaScript,Math)