????????????-???DecimalFormat??
?????????????????????????????λС???????????????д
????DecimalFormat df = new DecimalFormat("0.00");
???????????£?
????double d = 0.200;
????DecimalFormat df = new DecimalFormat("0.00");
????System.out.println(df.format(d));
?????????????
????0.20
??????double d=0.000?????????0.00??
??????double d=0?????????0.00??
??????double d=41.2345?????????41.23??
???????????????double d?????????????????????????λС????
???????????????λС??DecimalFormat df = new DecimalFormat("0.0");???????????
????????????д??DecimalFormat df = new DecimalFormat("0.00")???????????κ????????????λС??
???????????д??????????
????double d = 41.123;
????DecimalFormat df = new DecimalFormat("#.##");
????System.out.println(df.format(d));
?????????????
????41.12
??????double d=2.00?????????2??
??????double d=41.001?????????41??
??????double d=41.010?????????41.01??
??????double d=0?????????0??
??????double d=0.200?????????0.2??
???????????????д??DecimalFormat df = new DecimalFormat("#.##")??????С??????治?0????λС????????д????????????2?С?????????????λ?????0??
????????з???
?????????
????double d = 0.6544;
????String s=String.format("%.2f"??d);
????System.out.println(s);
?????????????
????0.65
??????double d=0.6566?????????0.66??
??????double d=0?????????0.00;
????????String s=String.format("%.2f"??d)???С???????????λС????????2??????λС???????????λС??????2???3?????????????
?????????????????????????????????????λС???????????????????????
?????????????-???BigDecimal??
?????????
????double d = 1.000;
????BigDecimal bd=new BigDecimal(d);
????double d1=bd.setScale(2??BigDecimal.ROUND_HALF_UP).doubleValue();
????System.out.println(d1);
??????????:1.0
??????double d=0?????????0.0??
??????double d=1.999?????????2.0??
??????double d=1.89?????????1.89??
??????????????д????С??????????????λС??????????????????????
???????
?????????Java??????
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
double d = 1.19;
System.out.println(formatDecimal4(d));
}
public static String formatDecimal1(double d) {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(d);
}
public static String formatDecimal2(double d) {
DecimalFormat df = new DecimalFormat("#.##");
return df.format(d);
}
public static String formatDecimal3(double d){
return String.format("%.2f"??d);
}
public static double formatDecimal4(double d){
BigDecimal bd=new BigDecimal(d);
double d1=bd.setScale(2??BigDecimal.ROUND_HALF_UP).doubleValue();
return d1;
}
}