Java 存储 20 位数字的数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25851786/
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
Datatype to store 20 digit number
提问by DeepVeen
I have a number of 20 digit, which datatype will support to store this number? I have tried long, double but I 'm getting out of range.
我有一个 20 位的数字,哪种数据类型将支持存储这个数字?我已经尝试了很长时间,但我超出了范围。
Number = 48565664968483514466
数字 = 48565664968483514466
Then I have to convert this number to Base36 to generate the barcode.
然后我必须将此数字转换为 Base36 以生成条形码。
回答by kirti
BigInteger:
The BigInteger
class allocates as much memory as it needs to hold all the bits of data it is asked to hold and also provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.
的BigInteger
,因为它需要保持它被要求保持数据的所有位,也分配类的内存提供操作类似物所有Java的基本整数运营商和所有相关方法java.lang.Math.
Declare it as
将其声明为
BigInteger bi1 = new BigInteger("12345678900123");
回答by brso05
BigInteger i = new BigInteger("48565664968483514466");
回答by Jim Garrison
when I'm trying to use new BigInteger(number), I'm getting literal of type int is out of range
当我尝试使用 new BigInteger(number) 时,我得到 int 类型的文字超出范围
The syntax you are looking for is
您正在寻找的语法是
BigInteger n = new BigInteger("48565664968483514466");
with the numeric literal as a String
, since the primitive integer literals cannot hold a number this large.
将数字文字作为 a String
,因为原始整数文字无法容纳这么大的数字。
回答by assylias
To convert your number in base 36:
以 36 为基数转换您的数字:
BigInteger number = new BigInteger("48565664968483514466");
String numberInBase36 = number.toString(36);
System.out.println(numberInBase36);