当前位置:   article > 正文

Java保留(2位)小数点的四种方法_java保留两位小数println

java保留两位小数println

1.String类format函数

double a=12.223;

System.out.println(String.format("%.2f",a));

2.BigDecimal

BigDecimal bg=new BigDecimal(d);

double d3=bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

System.out.println(d);

3.DecimalFormat类

DecimalFormat df=new DecimalFormat(".00");

System.out.println(df.format(d));

4.通过NumberFormat类

NumberFormat nf=NumberFormat.getNumberInstance();

nf.setMaximumFractionDigits(2);

System.out.println(nf.format(d));

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/630726
推荐阅读
相关标签
  

闽ICP备14008679号