1、方式一:利用DecimalFormat
double d1 = 21.43125;
DecimalFormat df = new DecimalFormat("##.000");
String d2 = df.format(d1);
d1 = Double.parseDouble(d2);
System.out.println("d1:"+d1);
2、方式二:利用BigDecimal
double dd1 = 12.43435;
BigDecimal bd = new BigDecimal(dd1);
dd1 = bd.setScale(2, 4).doubleValue();//四舍五入,保留两位
System.out.println("dd1:"+dd1);