Java小数点位数保留

DecimalFormat df = new DecimalFormat("0.00");
DecimalFormat df1 = new DecimalFormat("0.0#");
DecimalFormat df2 = new DecimalFormat("0.##");
DecimalFormat df3 = new DecimalFormat("#.##");

double a = 111.11;
double b = 111.10;
double c = 111.00;
double d = 110.00;
double e = 1.00;
double f = 0.10;
double g = 0.01;
double h = 0.00;
double i = 199.9999;

System.out.println("wb______0.00: " + df.format(a));
System.out.println("wb______0.00: " + df.format(b));
System.out.println("wb______0.00: " + df.format(c));
System.out.println("wb______0.00: " + df.format(d));
System.out.println("wb______0.00: " + df.format(e));
System.out.println("wb______0.00: " + df.format(f));
System.out.println("wb______0.00: " + df.format(g));
System.out.println("wb______0.00: " + df.format(h));
System.out.println("wb______0.00: " + df.format(i));

System.out.println("wb______0.0#: " + df1.format(a));
System.out.println("wb______0.0#: " + df1.format(b));
System.out.println("wb______0.0#: " + df1.format(c));
System.out.println("wb______0.0#: " + df1.format(d));
System.out.println("wb______0.0#: " + df1.format(e));
System.out.println("wb______0.0#: " + df1.format(f));
System.out.println("wb______0.0#: " + df1.format(g));
System.out.println("wb______0.0#: " + df1.format(h));
System.out.println("wb______0.0#: " + df1.format(i));

System.out.println("wb______0.##: " + df2.format(a));
System.out.println("wb______0.##: " + df2.format(b));
System.out.println("wb______0.##: " + df2.format(c));
System.out.println("wb______0.##: " + df2.format(d));
System.out.println("wb______0.##: " + df2.format(e));
System.out.println("wb______0.##: " + df2.format(f));
System.out.println("wb______0.##: " + df2.format(g));
System.out.println("wb______0.##: " + df2.format(h));
System.out.println("wb______0.##: " + df2.format(i));

System.out.println("wb______#.##: " + df3.format(a));
System.out.println("wb______#.##: " + df3.format(b));
System.out.println("wb______#.##: " + df3.format(c));
System.out.println("wb______#.##: " + df3.format(d));
System.out.println("wb______#.##: " + df3.format(e));
System.out.println("wb______#.##: " + df3.format(f));
System.out.println("wb______#.##: " + df3.format(g));
System.out.println("wb______#.##: " + df3.format(h));
System.out.println("wb______#.##: " + df3.format(i));


String s=String.format("%.2f",i);
System.out.println("wb______%.2f: " + s);

BigDecimal bd=new BigDecimal(i);
double d1=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println("wb______BigDecimal: " + d1);

 

 

wb______0.00: 111.11
wb______0.00: 111.10
wb______0.00: 111.00
wb______0.00: 110.00
wb______0.00: 1.00
wb______0.00: 0.10
wb______0.00: 0.01
wb______0.00: 0.00
wb______0.00: 200.00
wb______0.0#: 111.11
wb______0.0#: 111.1
wb______0.0#: 111.0
wb______0.0#: 110.0
wb______0.0#: 1.0
wb______0.0#: 0.1
wb______0.0#: 0.01
wb______0.0#: 0.0
wb______0.0#: 200.0
wb______0.##: 111.11
wb______0.##: 111.1
wb______0.##: 111
wb______0.##: 110
wb______0.##: 1
wb______0.##: 0.1
wb______0.##: 0.01
wb______0.##: 0
wb______0.##: 200
wb______#.##: 111.11
wb______#.##: 111.1
wb______#.##: 111
wb______#.##: 110
wb______#.##: 1
wb______#.##: 0.1
wb______#.##: 0.01
wb______#.##: 0
wb______#.##: 200
wb______%.2f: 200.00
wb______BigDecimal: 200.0

你可能感兴趣的:(java)