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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值