分数式舍入问题?
我是否做了一些非常错误的事情,或者 DecimalFormat 真的有舍入问题吗?
DecimalFormat format = new DecimalFormat("#.####");
format.setRoundingMode(RoundingMode.HALF_UP); // .5 up, .4 down?
format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH)); // . as decimal seperator
System.out.println(format.format(1.848751)); // 1.8488 -> thats correct
System.out.println(format.format(1.848750)); // 1.8487 -> that is not correct
System.out.println(format.format(1.84875)); // 1.8487 -> that is not correct
Am I doing something terribly wrong or does DecimalFormat really have an issue with rounding?
DecimalFormat format = new DecimalFormat("#.####");
format.setRoundingMode(RoundingMode.HALF_UP); // .5 up, .4 down?
format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH)); // . as decimal seperator
System.out.println(format.format(1.848751)); // 1.8488 -> thats correct
System.out.println(format.format(1.848750)); // 1.8487 -> that is not correct
System.out.println(format.format(1.84875)); // 1.8487 -> that is not correct
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论