// Java Double 取整示例
public class DoubleRoundingExample {
public static void main(String[] args) {
double number = 123.456;
// 1. 向下取整 (floor)
double floorValue = Math.floor(number);
System.out.println("向下取整: " + floorValue); // 输出 123.0
// 2. 向上取整 (ceil)
double ceilValue = Math.ceil(number);
System.out.println("向上取整: " + ceilValue); // 输出 124.0
// 3. 四舍五入 (round)
long roundValue = Math.round(number);
System.out.println("四舍五入: " + roundValue); // 输出 123
// 4. 精确到小数点后几位并四舍五入
double roundedToTwoDecimalPlaces = Math.round(number * 100.0) / 100.0;
System.out.println("精确到小数点后两位: " + roundedToTwoDecimalPlaces); // 输出 123.46
}
}
double 类型,返回的是 long 类型的结果。希望这些示例能帮助你理解如何在 Java 中对 double 类型进行取整操作。
上一篇:java map 按key排序
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站