有两种情况:
1、只要输出结果的时候可以用以下方法:
double x1 = 0.026;
System.out.println(String.format("%.2f", x1));
结果:0.03
2、使用数据转换(4种方法)
//方案一:
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());
本文详细介绍在Java中如何精确控制浮点数的输出和转换,包括使用System.out.println结合String.format,以及通过Math.round、DecimalFormat、Double.parseDouble、BigDecimal等方法实现浮点数的精确到两位小数。
551

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



