java-Math ceil()、floor()、round()三个函数的区别

Math.ceil()、Math.floor()和Math.round()

这三个方法分别遵循下列舍入规则:
1. Math.ceil()返回大于等于参数x的最小整数,即对浮点数向上取整.

实例:

    println(Math.ceil(0))
    println(Math.ceil(0.4))
    println(Math.ceil(0.5))
    println(Math.ceil(0.7))
    println(Math.ceil(-1))
    println(Math.ceil(-0.2))
    println(Math.ceil(-0.6))

 

结果:

0.0
1.0
1.0
1.0
-1.0
-0.0
-0.0


2. Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;

    println(Math.floor(0))
    println(Math.floor(0.4))
    println(Math.floor(0.5))
    println(Math.floor(0.7))
    println(Math.floor(-1))
    println(Math.floor(-0.2))
    println(Math.floor(-0.6))

 

结果:

0.0
0.0
0.0
0.0
-1.0
-1.0
-1.0


3. Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。
 

    println(Math.round(0))
    println(Math.round(0.4))
    println(Math.round(0.5))
    println(Math.round(0.7))
    println(Math.round(-1))
    println(Math.round(-0.2))
    println(Math.round(-0.6))

结果:

0
0
1
1
-1
0
-1

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值