如何在Java中将数字四舍五入到n个小数位 [英] How to round a number to n decimal places in Java

查看:28
本文介绍了如何在Java中将数字四舍五入到n个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是一种将双精度数转换为使用加半法四舍五入的字符串的方法——即,如果要四舍五入的小数是 5,它总是四舍五入到下一个数字.这是大多数人在大多数情况下所期望的四舍五入的标准方法.

What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the standard method of rounding most people expect in most situations.

我也希望只显示有效数字 - 即不应该有任何尾随零.

I also would like only significant digits to be displayed - i.e. there should not be any trailing zeroes.

我知道这样做的一种方法是使用 String.format 方法:

I know one method of doing this is to use the String.format method:

String.format("%.5g%n", 0.912385);

返回:

0.91239

这很好,但是它总是显示带有 5 个小数位的数字,即使它们不重要:

which is great, however it always displays numbers with 5 decimal places even if they are not significant:

String.format("%.5g%n", 0.912300);

返回:

0.91230

另一种方法是使用DecimalFormatter:

DecimalFormat df = new DecimalFormat("#.#####");
df.format(0.912385);

返回:

0.91238

然而,正如您所见,这使用了半偶数舍入.也就是说,如果前一个数字是偶数,它将向下舍入.我想要的是这个:

However as you can see this uses half-even rounding. That is it will round down if the previous digit is even. What I'd like is this:

0.912385 -> 0.91239
0.912300 -> 0.9123

在 Java 中实现这一目标的最佳方法是什么?

What is the best way to achieve this in Java?

推荐答案

使用 setRoundingMode,设置RoundingMode 明确地用半偶数回合处理您的问题,然后使用您所需输出的格式模式.

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output.

示例:

DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) {
    Double d = n.doubleValue();
    System.out.println(df.format(d));
}

给出输出:

12
123.1235
0.23
0.1
2341234.2125

<小时>

EDIT:原始答案没有解决双精度值的准确性问题.如果您不太关心它是向上还是向下取整,那也没关系.但是,如果您想要精确舍入,则需要考虑值的预期精度.浮点值在内部具有二进制表示.这意味着像 2.7735 这样的值在内部实际上没有那个确切的值.它可以稍大或稍小.如果内部值稍小,则不会向上取整到 2.7740.要纠正这种情况,您需要了解您正在使用的值的准确性,并在四舍五入之前添加或减去该值.例如,当您知道您的值精确到 6 位时,然后要将中间值向上舍入,将该准确度添加到值中:


EDIT: The original answer does not address the accuracy of the double values. That is fine if you don't care much whether it rounds up or down. But if you want accurate rounding, then you need to take the expected accuracy of the values into account. Floating point values have a binary representation internally. That means that a value like 2.7735 does not actually have that exact value internally. It can be slightly larger or slightly smaller. If the internal value is slightly smaller, then it will not round up to 2.7740. To remedy that situation, you need to be aware of the accuracy of the values that you are working with, and add or subtract that value before rounding. For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:

Double d = n.doubleValue() + 1e-6;

要四舍五入,请减去准确度.

To round down, subtract the accuracy.

这篇关于如何在Java中将数字四舍五入到n个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆