简体   繁体   English

如何在java中为变量分配非常大的数字?

[英]How to assign very large numbers to variable in java?

I'm getting "integer number too large: 1000000000001" for the following code line. 对于以下代码行,我得到“整数过大:1000000000001”。 How do I make it so that maxValue can hold 1 quadrillion or 1 trillion? 我如何使maxValue可以容纳1千万亿或1万亿?

long maxValue = 1000000000001;      //1,000,000,000,001

You need to use a long literal (with an L at the end): 你需要使用一个长文字(最后有一个L ):

long maxValue = 1000000000001L; //1,000,000,000,001

Note that you don't need to use BigInteger if your numbers are between -2 63 and 2 63 -1 (inclusive). 请注意,如果您的数字介于-2 63和2 63 -1(含)之间,则无需使用BigInteger。 (2 63 -1 = 9223372036854775807L = 0x7fffffffffffffffL.) (2 63 -1 = 9223372036854775807L = 0x7fffffffffffffffL。)

This answer is complete. 这个答案是完整的。

Java Language Specification: Java语言规范:

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); 如果整数文字后缀为ASCII字母L或l(ell),则整数文字的长度为long; otherwise it is of type int (§4.2.1). 否则它的类型为int(§4.2.1)。

You may check for another question and answer before you ask. 您可以在询问之前检查另一个问题和答案。 Someone probably has the same question and got the best answer. 有人可能有同样的问题并得到了最好的答案。 See this: 看到这个:

Large Numbers in Java Java中的大数字

This is probably what you need. 这可能就是你所需要的。

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

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