使用DecimalFormat方法先设置一个格式,然后在输出时对double使用,实际上没有改变double的值,使用的四舍五入的方式。
public class Test {
public static void main(String[] args) {
// 方法一:输出时对double进行格式化保留两位小数 四舍五入
DecimalFormat df = new DecimalFormat("0.00");
double d1 = 3.23556;
System.out.println(df.format(d1));
}
}
原文:https://www.cnblogs.com/wxhnjh/p/11543334.html