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

编程入门 行业动态 更新时间:2024-10-15 10:16:19

Java ceil(), floor(), round()方法<a href=https://www.elefans.com/category/jswz/34/1770044.html style=详解"/>

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

更多推荐

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

本文发布于:2024-03-05 19:59:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1713264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详解   方法   Java   ceil   floor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!