我的博客
欢迎来到我的博客
bunny.icu

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

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

Math.floor() 是向下取整
Math.ceil() 是向上取整
Math.round() 是取最接近的整数,算法为Math.floor(x+0.5)

double a = 11.5;
double b = -11.5;

System.out.println(Math.floor(a));  // 11.0
System.out.println(Math.floor(b));  // -12.0
System.out.println(Math.ceil(a));   //  12.0
System.out.println(Math.ceil(b));   // -11.0
System.out.println(Math.round(a));  // 12
System.out.println(Math.round(b));  // -11

Reference

Java round() 方法 – 菜鸟教程

版权声明


本作品系原创, 转载须遵循 CC BY-NC-ND 4.0 许可协议
本文标题:Math.floor()、ceil()和round()
本文链接:https://www.bunny.icu/archives/1463

推荐文章

发表评论

textsms
account_circle
email

bunny.icu

Math.floor()、ceil()和round()
Math.floor() 是向下取整 Math.ceil() 是向上取整 Math.round() 是取最接近的整数,算法为Math.floor(x+0.5) double a = 11.5; double b = -11.5; System.out.println(Math.floor(a…
扫描二维码继续阅读
2022-08-02