在Java中,可以使用DecimalFormat类来保留两位小数。以下是一个示例代码:

import java.text.DecimalFormat;

public class Main {
    public static void main(String[] args) {
        double number = 3.1415926;
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        String formattedNumber = decimalFormat.format(number);
        System.out.println(formattedNumber);
    }
}

输出结果为:3.14

java保留两位小数

原文地址: https://www.cveoy.top/t/topic/iYnk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录