DecimalFormat df = new DecimalFormat("#####0.00");
String do=df.format(d);//返回的是String类型的数据
- //小数四舍五入
- public Double roundDouble(double dou, int i)
- {
- Double d= null;
- try
- {
- double factor = Math.pow(10, i);
- d= Math.floor(dou* factor + 0.5) / factor;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return d;
- }
本文介绍了一种使用Java实现小数四舍五入的方法,并展示了如何利用DecimalFormat类对数字进行格式化处理,确保输出的小数保持一致性和易读性。
1035

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



