使用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));
}
}
