double四舍五入问题 求解

BigDecimal decimal = new BigDecimal(0.005);
        BigDecimal decimal2 = new BigDecimal(0.015);

        DecimalFormat   df   =new   DecimalFormat("0.##");
        df.setRoundingMode(RoundingMode.HALF_UP);
        System.out.println(df.format(decimal.doubleValue()));
        System.out.println(df.format(decimal2.doubleValue())); 

输出的答案都是0.01 为什么,我希望第二个输入的是0.02

new BigDecimal最好用string,你直接写数字的话,结果不是你想要的,
这样:BigDecimal decimal2 = new BigDecimal("0.015");

如果需要保证精度,最好是不要使用BigDecimal的double参数的构造函数,因为存在损失double参数精度的可能,最好是使用BigDecimal的String参数的构造函数。最好是杜绝使用BigDecimal的double参数的构造函数。

你看错了吧,为啥我运行出来的就是;
decimal1=0.01
decimal2=0.02

new BigDecimal最好用string,你直接写数字的话,结果不是你想要的,
这样:BigDecimal decimal2 = new BigDecimal("0.015");
如果需要保证精度,最好是不要使用BigDecimal的double参数的构造函数,因为存在损失double参数精度的可能,最好是使用BigDecimal的String参数的构造函数。最好是杜绝使用BigDecimal的double参数的构造函数。

没问题啊,代码测试结果就是0.02啊
图片说明

decimal Round(decimal d, int decimals, MidpointRounding mode)
可以试试这个能不能用