使用DecimalFormat类
import java.text.DecimalFormat;
//0.00表示保留两位小数,该方法默认四舍五入
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(0.24544));//0.25
System.out.println(df.format(0.24444));//0.24
这篇博客介绍了Java中DecimalFormat类的使用方法,通过示例展示了如何保留两位小数并进行四舍五入操作。示例代码显示,0.24544会被格式化为0.25,而0.24444则保持为0.24,强调了格式化过程中的精度控制。
使用DecimalFormat类
import java.text.DecimalFormat;
//0.00表示保留两位小数,该方法默认四舍五入
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(0.24544));//0.25
System.out.println(df.format(0.24444));//0.24
1万+
9701
1万+

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