Datatype to store 20 digit number

烈酒焚心 提交于 2019-11-27 07:49:34

问题


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.

Number = 48565664968483514466

Then I have to convert this number to Base36 to generate the barcode.


回答1:


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.

Declare it as

BigInteger bi1 =  new BigInteger("12345678900123");



回答2:


To convert your number in base 36:

BigInteger number = new BigInteger("48565664968483514466");
String numberInBase36 = number.toString(36);
System.out.println(numberInBase36);



回答3:


when I'm trying to use new BigInteger(number), I'm getting literal of type int is out of range

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.




回答4:


BigInteger i = new BigInteger("48565664968483514466");


来源:https://stackoverflow.com/questions/25851786/datatype-to-store-20-digit-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!