// Java取整示例代码
public class RoundExample {
public static void main(String[] args) {
double num = 12.75;
// 使用 Math.floor() 向下取整
int floorValue = (int) Math.floor(num);
System.out.println("向下取整: " + floorValue); // 输出 12
// 使用 Math.ceil() 向上取整
int ceilValue = (int) Math.ceil(num);
System.out.println("向上取整: " + ceilValue); // 输出 13
// 使用 Math.round() 四舍五入取整
int roundValue = (int) Math.round(num);
System.out.println("四舍五入取整: " + roundValue); // 输出 13
}
}
Math.floor():向下取整,返回小于或等于给定数的最大整数。Math.ceil():向上取整,返回大于或等于给定数的最小整数。Math.round():四舍五入取整,返回最接近的整数。上一篇:java中多个三目运算符怎么写?
下一篇:java assert用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站