下面直接上代码:
DecimalFormat df1 = new DecimalFormat("#.00");
DecimalFormat df2 = new DecimalFormat("######0.00");
double d1 = 1.0;
double d2 = 5;
double d3 = 0.11;
double d4 = 1.11;
System.out.println("df1:"+df1.format(d1)+" pk "+df2.format(d1));
System.out.println("df1:"+df1.format(d2)+" pk "+df2.format(d2));
System.out.println("df1:"+df1.format(d3)+" pk "+df2.format(d3));
System.out.println("df1:"+df1.format(d4)+" pk "+df2.format(d4));
运行结果:
df1:1.00 pk 1.00
df1:5.00 pk 5.00
df1:.11 pk 0.11
df1:1.11 pk 1.11
当double值小于1时df1会将小数点之前的0省略,而df2不会.也算是他们之间的差别吧,坑以填平.