java 小数点取2位并且四舍五入

时间:2022-11-03 21:08:52
BigDecimal bd = new BigDecimal(0.0199999999999);
System.out.println("res:"+bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
         DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(Double.parseDouble(str)));
System.out.println("=================");
System.out.println(String.format("%.2f", Double.parseDouble(str)));
System.out.println("=================");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(Double.parseDouble(str)));