[英]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: 我在这里看到几个问题:
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.format
和Double.parseDouble
的组合容易出错,例如在德语语言环境中, DecimalFormat.format
将使用逗号作为小数点分隔符, Double.parseDouble
不支持该分隔符,并会引发NumberFormatException
。 You should use both format
and parse
of your DecimalFormat. format
和parse
。 System.out.println
. 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));
Math.round
. Math.round
。 You can multiply and divide by 10, 100, 1000 etc. to achieve the desired precision. 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 对于大数,您需要使用BigInteger或BigDecimal
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.