我可以使用什么变量类型来保存 Java 中的大数字(30 位以上)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18444337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What variable type can I use to hold huge numbers (30+ digits) in java?
提问by Jonathan Lam
Is there a really large variable type I can use in Java to store huge numbers (up to around forty digits)?
我可以在 Java 中使用一个非常大的变量类型来存储大量数字(最多大约 40 位数字)吗?
long
's maximum value is 9223372036854775807, which is 19 digits -- not nearly large enough.
long
的最大值是 9223372036854775807,它是 19 位数字——不够大。
I'm trying to create a calculator that can handle large numbers, because most nowadays can only hold an insufficient 10 digits or so, and I want want accurate calculations with numbers of a much larger magnitude
我正在尝试创建一个可以处理大量数字的计算器,因为现在大多数数字只能容纳不足 10 位左右,我想要对更大数量级的数字进行准确计算
EDIT
编辑
Thanks for the answers. I can use BigInteger
for big integers, the only limit being the computer's memory (should be sufficient). For decimals, I'll use float
^e, as @WebDaldo suggested, or BigDecimal
(similar to BigInteger), as @kocko suggested.
感谢您的回答。我可以BigInteger
用于大整数,唯一的限制是计算机的内存(应该足够了)。对于小数,我将使用float
^e,如@WebDaldo 建议的那样,或BigDecimal
(类似于 BigInteger),如@kocko 建议的那样。
采纳答案by Sajal Dutta
You can use BigIntegerclass.
您可以使用BigInteger类。
BigInteger bi1 = new BigInteger("637824629384623845238423545642384");
BigInteger bi2 = new BigInteger("3039768898793547264523745379249934");
BigInteger bigSum = bi1.add(bi2);
BigInteger bigProduct = bi1.multiply(bi2);
System.out.println("Sum : " + bigSum);
System.out.println("Product : " + bigProduct);
Output:
输出:
Sum : 3677593528178171109762168924892318
Product : 1938839471287900434078965247064711159607977007048190357000119602656
总和:3677593528178171109762168924892318
产品 : 1938839471287900434078965247064711159607977007048190357000119602656
I should mention BigDecimal, which is excellent for amount calculations compare to double.
我应该提到BigDecimal,与double相比,它非常适合金额计算。
BigDecimal bd = new BigDecimal("123234545.4767");
BigDecimal displayVal = bd.setScale(2, RoundingMode.HALF_EVEN);
NumberFormat usdFormat = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(usdFormat.format(displayVal.doubleValue()));
Output:
输出:
$123,234,545.48
123,234,545.48 美元
回答by Konstantin Yovkov
You can try using the BigIntegerclass for operations with really huge integer numbers.
您可以尝试使用BigInteger类来进行非常大的整数运算。
For operations with floating numbers, Java provides the BigDecimalclass, which can be useful, as well.
对于浮点数运算,Java 提供了BigDecimal类,它也很有用。
回答by JavaDM
You can use float ^e
您可以使用浮动 ^e
so you could have
所以你可以有
0.55342663552772737682136182736127836782163 * 10^e
Calculators are mostly use that, too.
计算器也大多使用它。
回答by Joni
For calculations with exponents, like you would use in a calculator, you should use BigDecimal
. The problem with BigInteger
is that it only handles integers (no fractional numbers) and that for really big numbers like 10^100 it stores all the zeros, using a lot of memory, instead of using a format based on scientific notation.
对于指数计算,就像您在计算器中使用的那样,您应该使用BigDecimal
. 问题BigInteger
在于它只处理整数(没有小数),对于像 10^100 这样的大数字,它使用大量内存存储所有零,而不是使用基于科学计数法的格式。
You could alternatively use the floating point number type double
, which gives you a large range of values, low memory usage and fast operations. But because of rounding issues and limited precision (around 16 decimal digits), I wouldn't recommend using it unless you really know what you're doing.
您也可以使用浮点数类型double
,它为您提供大范围的值、低内存使用率和快速操作。但是由于舍入问题和有限的精度(大约 16 位十进制数字),除非您真的知道自己在做什么,否则我不建议使用它。
回答by kamals1986
This is for all bigger numbers above 15 since using int blows it. You may want to find the factorial of 50 or 100 0r 500.
这适用于 15 以上的所有更大的数字,因为使用 int 会破坏它。您可能想要找到 50 或 100 0r 500 的阶乘。
// Recursive version of the Fat factorial for bigger numbers ex: Factorial of 500
BigInteger fatFactorial(int b) {
if (BigInteger.ONE.equals(BigInteger.valueOf(b))
|| BigInteger.ZERO.equals(BigInteger.valueOf(b))) {
return BigInteger.ONE;
} else {
return BigInteger.valueOf(b).multiply(fatFactorial(b - 1));
}
}