Java 如何存储一个大(10 位)整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1938855/
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
How to store a large (10 digits) integer?
提问by silverkid
Which Java data type would be able to store a big numerical value, like 9999999999?
哪种 Java 数据类型能够存储大数值,例如 9999999999?
采纳答案by Bozho
Your concrete example could be stored in long
(or java.lang.Long
if this is necessary).
您的具体示例可以存储在long
(或者java.lang.Long
如果有必要)。
If at any point you need bigger numbers, you can try
java.math.BigInteger
(if integer), or java.math.BigDecimal
(if decimal)
如果在任何时候您需要更大的数字,您可以尝试
java.math.BigInteger
(如果是整数)或java.math.BigDecimal
(如果是小数)
回答by Ben James
You can store this in a long
. A long
can store a value from -9223372036854775808
to 9223372036854775807
.
您可以将其存储在long
. Along
可以存储从-9223372036854775808
到的值9223372036854775807
。
回答by Thilo
A primitive long or its java.lang.Longwrapper can also store ten digits.
原始 long 或其java.lang.Long包装器也可以存储十位数字。
回答by akellakarthik
you can use long or double.
您可以使用 long 或 double。
回答by Joachim Sauer
In addition to all the other answers I'd like to note that if you want to write that number as a literal in your Java code, you'll need to append a L
or l
to tell the compiler that it's a long
constant:
除了我想指出的所有其他答案之外,如果您想在 Java 代码中将该数字写为文字,则需要附加 aL
或l
告诉编译器它是一个long
常量:
long l1 = 9999999999; // this won't compile
long l2 = 9999999999L; // this will work
回答by Clovis
You could store by creating an object that hold a string value number to store in an array list.
by example: BigInt objt = new BigInt("999999999999999999999999999999999999999999999999999");
您可以通过创建一个包含字符串值编号的对象来存储以存储在数组列表中。举例:BigInt objt = new BigInt("999999999999999999999999999999999999999999999999999");
objt is created by the constructor of BigInt class. Inside the class look like.
objt 是由 BigInt 类的构造函数创建的。类里面的样子。
BigInt{
ArrayList<Integer> myNumber = new ArrayList <Integer>();
public BigInt(){}
public BigInt(String number){ for(int i; i<number.length; i++){ myNumber.add(number.indexOf(i)); } }
}
回答by ravi ranjan
Use BigInt datatype with its implicit operations. The plus point for it is it will not give answers in exponential representation. It will give full length result
使用 BigInt 数据类型及其隐式操作。它的优点是它不会以指数表示形式给出答案。它将给出全长结果
Here is an example of addition
这是一个加法的例子
BigInteger big1 = new BigInteger("1234567856656567242177779");
BigInteger big2 = new BigInteger("12345565678566567131275737372777569");
BigInteger bigSum = big1.add(big2);
System.out.println(bigSum );
回答by Nabin Kumar Khatiwada
A wrapper class java.lang.Longcan store 10 digit easily.
包装类java.lang.Long可以轻松存储 10 位数字。
Long phoneNumber = 1234567890;
It can store more than that also.
它还可以存储更多。
Documentation:
文档:
public final class Long extends Number implements Comparable<Long> {
/**
* A constant holding the minimum value a {@code long} can
* have, -2<sup>63</sup>.
*/
@Native public static final long MIN_VALUE = 0x8000000000000000L;
/**
* A constant holding the maximum value a {@code long} can
* have, 2<sup>63</sup>-1.
*/
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
}
This means it can store values of range 9,223,372,036,854,775,807to -9,223,372,036,854,775,808.
这意味着它可以存储范围9,223,372,036,854,775,807到-9,223,372,036,854,775,808 的值。