[英]How to get an integer value that is greater than the maximum integer value
I know that in Java an int can get a value of 2,147,483,647. 我知道在Java中int可以得到2,147,483,647。 But I want more value than that.
但是我想要更多的价值。 I have a formula for example:
我有一个公式,例如:
double x = a/(b*c);
So the denominator (b*c) can reach to 10^10 or maybe even higher than that. 因此分母(b * c)可以达到10 ^ 10甚至更高。 But whenever I execute the formula, the value is always limited to 2,147,483,647.
但是无论何时执行公式,该值始终限制为2,147,483,647。 I know because x must always be smaller than 1.0.
我知道,因为x必须始终小于1.0。 P/S: Even variable "a" can also reach 10^10 if some conditions are satisfied.
P / S:如果满足某些条件,即使变量“ a”也可以达到10 ^ 10。 a,b,c are all integer numbers.
a,b,c都是整数。
Since you asked for it, here's a BigInteger
example: 自您提出要求以来,这里有一个
BigInteger
示例:
BigInteger a = new BigInteger("yourNumberInStringFormA");
BigInteger b = new BigInteger("yourNumberInStringFormB");
BigInteger c = new BigInteger("yourNumberInStringFormC");
BigInteger x = a.divide(b.multiply(c));
Where "yourNumberInStringForm"
is an optional minus sign and numbers only (no whitespace or commas). 其中
"yourNumberInStringForm"
是可选的减号和仅数字(无空格或逗号)。 For example BigInteger z = new BigIntger("-3463634");
例如,
BigInteger z = new BigIntger("-3463634");
NB: BigInteger
will actually return a long
for you if your number is in its range. 注意:如果您的数字在其范围内,则
BigInteger
实际上会为您返回一个long
。 longs
end in L
, as in: longs
为L
,如:
long num = 372036854775807L;
The max length for a long
is: 9,223,372,036,854,775,807. 一个最大长度
long
是:9,223,372,036,854,775,807。 If your numbers are going to be less than that, it'll make your life a ton easier to use long
or Long
, its wrapper, over BigInteger
. 如果您的数字要少于这个数字,那么使用
BigInteger
long
或Long
可以使您的生活更加轻松。 Since with long
, you don't have to use methods for dividing/multiplying, etc. 从
long
,您不必使用除法/乘法等方法。
The max int value is a java language constant. max int值是Java语言常数。 If you want real numbers larger than what
int
provides, use long . 如果要使实数大于
int
提供的实数,请使用long 。 If you need integers larger than what int
and long
provide, you can use BigInteger
: 如果需要大于
int
和long
提供的整数,则可以使用BigInteger
:
BigInteger a = new BigInteger("9");
BigInteger b = new BigInteger("3");
BigInteger c = a.add(b); // c.equals(new BigInteger("12"), a and b are unchanged
BigInteger
is immutable just like Long
and Integer
, but you can't use the usual operator symbols. 就像
Long
和Integer
, BigInteger
是不可变的,但是您不能使用通常的运算符。 Instead use the methods provided by the class. 而是使用类提供的方法 。
I also notice you're ending up with a double
. 我还注意到您最终获得了
double
。 If it's okay to use doubles throughout your formula, it's worth noting that their max value is: 1.7976931348623157 E 308
如果可以在公式中使用双精度数,则值得注意的是它们的最大值是:
1.7976931348623157 E 308
Read more about java language constants . 阅读有关Java语言常量的更多信息 。
使用long类型,仍然是整数类型,但其最大值大于int
try this 尝试这个
double x = a / ((double) b * c);
a and c will be cast to double by java automatically. java将自动将a和c强制转换为double。 See http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 If either operand is of type double, the other is converted to double.
请参见http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 如果一个操作数的类型为double,则另一个将转换为double。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.