1.(double) (Math.round(sd3*10000)/10000.0);
这样为保持4位
(double) (Math.round(sd3*100)/100.0);
这样为保持2位.
2.另一种办法
import java.text.DecimalFormat;
DecimalFormat df2 = new DecimalFormat("###.00");
DecimalFormat df2 = new DecimalFormat("###.000");
System.out.println(df2.format(doube_var));
第一个为2位,第二个为3位.
这样为保持4位
(double) (Math.round(sd3*100)/100.0);
这样为保持2位.
2.另一种办法
import java.text.DecimalFormat;
DecimalFormat df2 = new DecimalFormat("###.00");
DecimalFormat df2 = new DecimalFormat("###.000");
System.out.println(df2.format(doube_var));
第一个为2位,第二个为3位.
本文介绍两种在Java中保留小数点后特定位数的方法:一是通过数学运算实现,二是利用DecimalFormat类格式化输出。适用于需要精确控制浮点数显示精度的场景。
528

被折叠的 条评论
为什么被折叠?



