小数点的几种精确方法
1.直接用格式化输出String.format()
double b=123.4; System.out.println(String.format("%.3f",b)); //打印结果为123.400//这里精确到小数点后三位 System.out.println(b);//打印结果为123.4
2.通过导入DecimalFormat包精确小数点
import java.text.DecimalFormat; DecimalFormat b1 = new DecimalFormat("0.000"); //这里还可写成DecimalFormat b1 = new DecimalFormat("#.###") System.out.println(b1.format(b));
3.通过导入Mathbao中的BigDecimal来实现精确小数点
import java.math.BigDecimal; BigDecimal bd = new BigDecimal(b); System.out.println(bd.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());