Double b = 5.0E-4;
BigDecimal db = new BigDecimal(b.toString());
String ii = db.toPlainString();
String s = new BigDecimal(ii).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
String format = String.format("%.2f",db );
System.out.println(b);
System.out.println(ii);
System.out.println(s);
System.out.println(format);
保留两位小数方法
方法一:String的format方法(推荐)
double f = 111231.5585;
System.out.println(String.format("%.2f", f));
方法二:DecimalFormat的format方法
double f = 111231.5585;
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(f));
//方法三:BigDecimal的setScale方法
double f = 111231.5585;
BigDecimal bg = new BigDecimal(f);
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);
//方法四:NumberFormat的setMaximumFractionDigits方法
double f = 111231.5585;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(f));