1.使用java.math.BigDecimal
double d = 2.345;
BigDecimal bigDecimal = new BigDecimal(d);
//四舍五入了
BigDecimal bigDecimal1 = bigDecimal.setScale(2, RoundingMode.HALF_UP);
System.out.println(bigDecimal1);//2.35
2.使用java.text.DecimalFormat
double ave = 109.3456;
//四舍五入了
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(ave));//109.23
3.使用java.text.NumberFormat
double ave1 = 109.3456;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);
nf.setGroupingUsed(false);
System.out.println(nf.format(ave1));//109.35
4.使用java.util.Formatter
double ave2 = 109.3456;
String s = new Formatter().format("%.2f", ave2).toString();
System.out.println(s);//109.35
5.使用String.format(推荐)
double ave3 = 109.3456;
System.out.println(String.format("%.2f",ave3));//109.35