1.只要输出结果
1
2
double x1 = 0.026;
System.out.println(String.format("%.2f", x1));
2.数据转换
1
2
3
4
5
6
7
8
9
10
11
//方案一:
get_double = (double)(Math.round(result_value*100)/100.0)
//方案二:
DecimalFormat df = new DecimalFormat("#.##");
get_double = Double.ParseDouble(df.format(result_value));
//方案三:
get_double = Double.ParseDouble(String.format("%.2f",result_value));
//方案四:
BigDecimal bd = new BigDecimalresult_value();
BigDecimal bd2 = bd.setScale(2,BigDecimal .ROUND_HALF_UP);
get_double = Double.ParseDouble(bd2.ToString());
3.只保留两位小数(不含四舍五入)
1
2
double d = 13.4324;
d=((int)(d*100))/100;
1
2
double x1 = 0.026;
System.out.println(String.format("%.2f", x1));
2.数据转换
1
2
3
4
5
6
7
8
9
10
11
//方案一:
get_double = (double)(Math.round(result_value*100)/100.0)
//方案二:
DecimalFormat df = new DecimalFormat("#.##");
get_double = Double.ParseDouble(df.format(result_value));
//方案三:
get_double = Double.ParseDouble(String.format("%.2f",result_value));
//方案四:
BigDecimal bd = new BigDecimalresult_value();
BigDecimal bd2 = bd.setScale(2,BigDecimal .ROUND_HALF_UP);
get_double = Double.ParseDouble(bd2.ToString());
3.只保留两位小数(不含四舍五入)
1
2
double d = 13.4324;
d=((int)(d*100))/100;
本文介绍了几种在Java中实现浮点数输出的方法,包括使用System.out.println结合String.format进行格式化输出,利用Math.round进行数值舍入,通过DecimalFormat定制显示格式,运用Double.parseDouble配合String.format精确控制输出,以及采用BigDecimal进行高精度算术运算并设置小数位数。
5078

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



