我使用的是jdk 1.8.0_45,我们的测试发现了一个错误.
当决定舍入的最后一个小数为5时,RoundingMode.HALF_DOWN与RoundingMode.HALF_UP的工作方式相同.
我发现了RoundingMode.HALF_UP的相关问题,但它们在更新40中得到修复.我还在oracle中添加了一个bug,但根据我的经验,它们确实没有响应.
package testjava8;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class Formatori {
public static void main(String[] args) {
DecimalFormat format = new DecimalFormat("#,##0.0000");
format.setRoundingMode(RoundingMode.HALF_DOWN);
Double toFormat = 10.55555;
System.out.println("Round down");
System.out.println(format.format(toFormat));
format.setRoundingMode(RoundingMode.HALF_UP);
toFormat = 10.55555;
System.out.println("Round up");
System.out.println(format.format(toFormat));
}
}
实际结果:
向下舍入
10.5556
围捕
10.5556
预期结果(使用jdk 1.7获得):
向下舍入
10.5555
围捕
10.5556