温馨提示×

java向上取整怎么保留两位小数输出

小亿
108
2024-01-18 13:10:02
栏目: 编程语言

在Java中,可以使用Math.ceil()函数向上取整,并使用DecimalFormat类将结果保留两位小数输出。

以下是一个示例代码:

import java.text.DecimalFormat;

public class Main {
    public static void main(String[] args) {
        double number = 10.2345;
        
        // 向上取整
        double ceilNumber = Math.ceil(number);
        
        // 保留两位小数输出
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        String formattedNumber = decimalFormat.format(ceilNumber);
        
        System.out.println(formattedNumber);
    }
}

运行以上代码,输出结果为:11.00

0