- 方法1
BigDecimal bd = new BigDeciaml(variable).setScale(4, BigDecimal.ROUND_HALF_UP);
double result1 = db.doubleValue();
- 方法2DecimalFormat不仅可以保留小数位,还可以用作转化小数为百分数
DecimalFormat df = new DecimalFormat("#.0000");
String result2 = df.format(variable);
- 方法3
String result3 = String.format("%.4f", variable);
- 方法4
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(4);
String result4 = nf.format(variable);