我在Java中遇到四舍五入的问题(例如,结果取整= 1387.583515625,输出结果是x = 5836而不是5835。我之前搜索过该问题,并且尝试过各种答案,但似乎还是看不到它。工作,有什么想法吗?

DecimalFormat df = new DecimalFormat(".####");
df.setRoundingMode(RoundingMode.CEILING);

int x = Integer.parseInt(String.format(df.format(whole)).split("\\.")[1]);

最佳答案

查看此表以确定您想要什么https://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html

df.setRoundingMode(RoundingMode.HALF_UP);

关于java - Java中的舍入错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35271305/

10-12 02:17