简体   繁体   English

如何在Java中舍入一个大的十进制值?

[英]How to round a large decimal value in Java?

I'm trying in the following code to round a decimal number based on the decimal format that I set at the beginning: 我正在尝试在以下代码中根据在开始时设置的十进制格式舍入十进制数字:

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

double a = Double.parseDouble(df.format(43.473684210526315));
double b = Math.pow(a, 10);
double c = Double.parseDouble(df.format(b));

System.out.println(a + " ** " + b  +  " ** " + c);

The result that I got is: 我得到的结果是:

43.473 ** 2.4109939006965688E16 ** 2.4109939006965688E16

As you see, the value of a is formatted properly. 正如你看到的,值了a具有正确的格式。 While the other values are not. 而其他值则不然。 After spending a long time trying to understand what is going on, I found out the value 2.4109939006965688E16 can not be formatted. 花了很长时间试图了解正在发生的事情之后,我发现值2.4109939006965688E16无法格式化。 I tested the same value after removing E16 and it worked. 删除E16之后,我测试了相同的值,并且可以正常工作。

My question is how can I round such a large decimal so that it works as the a ? 我的问题是如何舍入这么大的小数点,使其像a

对于“大”小数,可以使用以下格式:

DecimalFormat df2 = new DecimalFormat("#.000E0"); 

I see several issues here: 我在这里看到几个问题:

  • You do not specify a locale for your DecimalFormat. 您没有为DecimalFormat指定语言环境。 So the combination of DecimalFormat.format and Double.parseDouble is error prone, as eg with german locale, DecimalFormat.format will use a comma as decimal separator, which is unsupported by Double.parseDouble , and will throw a NumberFormatException . 因此, DecimalFormat.formatDouble.parseDouble的组合容易出错,例如在德语语言环境中, DecimalFormat.format将使用逗号作为小数点分隔符, Double.parseDouble不支持该分隔符,并会引发NumberFormatException You should use both format and parse of your DecimalFormat. 您应该同时使用DecimalFormat的formatparse
  • Also, you use your double values directly for System.out.println . 另外,您直接将Double值用于System.out.println Why not concatenating the results of DecimalFormat.format , to achieve exactly what you want? 为什么不连接DecimalFormat.format的结果来实现您想要的结果呢? Eg System.out.println(df.format(a) + " ** " + df.format(b) + " ** " + df.format(c)); 例如System.out.println(df.format(a) + " ** " + df.format(b) + " ** " + df.format(c));
  • If you want to do real mathematical rounding (eg needing the double value for additional calculations), use Math.round . 如果要进行真正的数学舍入(例如,需要使用double值进行其他计算),请使用Math.round You can multiply and divide by 10, 100, 1000 etc. to achieve the desired precision. 您可以乘以10、100、1000等,以达到所需的精度。 Note that, with the double data type, you can't do exact rounding. 请注意,使用double数据类型不能进行精确的舍入。

It's pure luck that a works. 这是纯粹的运气是a作品。 If the initial value was 43.4701 you would be seeing 43.47 . 如果初始值为43.4701您将看到43.47 And in some cases you would see more than 3 decimal places due to the inaccuracy of double . 在某些情况下,由于double的不准确,您会看到超过3个小数位。

You only use the DecimalFormatter to create a String briefly before turning back into a double again. 您只能使用DecimalFormatter简短地创建一个String然后再将其再次变为double You want to keep and use that string. 您要保留并使用该字符串。

DecimalFormat df = new DecimalFormat("#.000");

double b = Math.pow(43.473684210526315, 10);

String bFormatted = df.format(b);

System.out.println(bFormatted);

Gives you your desired output 24115485538109308.000 给您所需的输出24115485538109308.000

For large numbers, you'll need to use BigInteger or BigDecimal 对于大数,您需要使用BigIntegerBigDecimal

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

BigDecimal a = new BigDecimal(43.473);
BigDecimal b = a.pow(10);

System.out.println(df.format(a) + " ** " + b.doubleValue() + " ** " + df.format(b));

This results in the following output 结果为以下输出

43.472 ** 2.4109939006965688E16 ** 24109939006965686.655

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM