double a = 123.456789; double b = 123.444444; String sa = String.format("%.2f",a); System.out.println(sa); String sb = String.format("%.2f",b); System.out.println(sb);
//输出 123.46 123.44
//简写 System.out.println(String.format("%.2f",a));
2.DecimalFormat的format方法
1 2 3 4 5 6 7 8 9 10 11 12 13
double a = 123.456789; double b = 123.444444; DecimalFormat dfa = new DecimalFormat("0.00"); System.out.println(dfa.format(a)); DecimalFormat dfb = new DecimalFormat("0.00"); System.out.println(dfb.format(b));