Java ceil(), floor(), round()方法详解

目录

ceil()方法

floor()方法

round()方法 


ceil()方法

        ceil() 方法可对一个数进行上舍入,返回值大于等于( >= )给定参数的的最小整数,类型为双精度浮点型。

实例如下:

double a = 1.65;
double b = -9.1;
double c = -6.0;
System.out.println("ceil(a) = "+Math.ceil(a));
System.out.println("ceil(b) = "+Math.ceil(b));
System.out.println("ceil(c) = "+Math.ceil(c));

结果:

ceil(a) = 2.0
ceil(b) = -9.0
ceil(c) = -6.0

floor()方法

        floor() 方法可对一个数进行下舍入,返回给定参数最大的整数,该整数小于或等于(<=)给定的参数。

实例如下:

double a = 1.65;
double b = -9.1;
double c = -6.0;
System.out.println("floor(a) = "+Math.floor(a));
System.out.println("floor(b) = "+Math.floor(b));
System.out.println("floor(c) = "+Math.floor(c));

输出:

floor(a) = 1.0
floor(b) = -10.0
floor(c) = -6.0

round()方法 

        round() 方法返回一个最接近的 int、long 型值,四舍五入。round 表示"四舍五入",算法为Math.floor(x+0.5) ,即将原来的数字加上 0.5 后再向下取整,所以 Math.round(11.5) 的结果为 12,Math.round(-11.5) 的结果为 -11。

实例如下:

double a = 1.65;
double b = -9.1;
double c = -6.0;
System.out.println("round(a) = "+Math.round(a));
System.out.println("round(b) = "+Math.round(b));
System.out.println("round(c) = "+Math.round(c));

输出:

round(a) = 2
round(b) = -9
round(c) = -6

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值