java 将 long 解析为负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3957937/
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
parse long to negative number
提问by hguser
code:
代码:
public class Main{
public static void main(String[] a){
long t=24*1000*3600;
System.out.println(t*25);
System.out.println(24*1000*3600*25);
}
}
This prints :
这打印:
2160000000
-2134967296
Why?
为什么?
Thanks for all the replies.
感谢所有的答复。
Is the only way to use L after the number?
是在数字后使用 L 的唯一方法吗?
I have tried the (long)24*1000*3600*25
but this is also negative.
我已经尝试过,(long)24*1000*3600*25
但这也是负面的。
回答by Alberto Zaccagni
You reached the max of the int
type which is Integer.MAX_VALUE
or 2^31-1. It wrapped because of this, thus showing you a negative number.
您达到了或 2^31-1int
类型的最大值Integer.MAX_VALUE
。它因此被包裹,从而向您显示负数。
For an instant explanation of this, see this comic:
有关此问题的即时解释,请参阅此漫画:
回答by Adeel Ansari
To explain it clearly,
为了解释清楚,
System.out.println(24*1000*3600*25);
In the above statement are actually int
literals. To make treat them as a long
literal you need to suffix those with L
.
在上面的语句中实际上是int
文字。要将它们视为long
文字,您需要为它们添加后缀L
.
System.out.println(24L*1000L*3600L*25L);
Caveat, a small l
will suffice too, but that looks like capital I
or 1
, sometimes. Capital I
doesn't make much sense here, but reading that as 1
can really give hard time. Furthermore, Even sufficing a single value with L
will make the result long
.
警告,一个小的l
也足够了,但这看起来像资本,I
或者1
,有时。资本I
在这里没有多大意义,但阅读它1
真的很难。此外,即使有足够的单个值L
也会使结果long
.
回答by Lasse Espeholt
In the first case you are printing a long
but in the second, you are printing it as int
.
在第一种情况下,您正在打印 a,long
但在第二种情况下,您将其打印为int
.
And int
has a range from: -2^31 to 2^31 - 1 which is just below what you are calculating (int max: 2147483647 you: 2160000000) so you overflow the int to the negative range.
并且int
范围从:-2^31 到 2^31 - 1 这正好低于您正在计算的值(int max:2147483647 你:2160000000)所以你将 int 溢出到负范围。
You can force the second one to use long
as well:
您也可以强制使用第二个long
:
System.out.println(24L*1000*3600*25);
回答by Sagar V
You should suffix the numbers with 'l'. Check the snippet below:
您应该在数字后加上“l”。检查下面的片段:
public static void main(String[] a){
long t=24*1000*3600;
System.out.println(t*25);
System.out.println(24l*1000l*3600l*25l);
}
回答by dogbane
Integral literals are treated as type int
by default. 24*1000*3600*25
is greater than Integer.MAX_VALUE
so overflows and evaluates to -2134967296. You need to explicitly make one of them a long
using the L suffix to get the right result:
int
默认情况下,整型文字被视为类型。24*1000*3600*25
大于Integer.MAX_VALUE
so 溢出并计算为 -2134967296。您需要long
使用 L 后缀明确地将其中一个设为 a以获得正确的结果:
System.out.println(24L*1000*3600*25);
回答by dinidu
If you want to do mathematical operations with large numerical values without over flowing, try the BigDecimal class.
如果您想在不溢出的情况下对大数值进行数学运算,请尝试使用 BigDecimal 类。
Let's say I want to multiply
假设我想乘以
200,000,000 * 2,000,000,000,000,000,000L * 20,000,000
200,000,000 * 2,000,000,000,000,000,000L * 20,000,000
int testValue = 200000000;
System.out.println("After Standard Multiplication = " +
testValue *
2000000000000000000L *
20000000);
The value of the operation will be -4176287866323730432, which is incorrect.
该操作的值将是-4176287866323730432,这是不正确的。
By using the BigDecimal class you can eliminate the dropped bits and get the correct result.
通过使用 BigDecimal 类,您可以消除丢失的位并获得正确的结果。
int testValue = 200000000;
System.out.println("After BigDecimal Multiplication = " +
decimalValue.multiply(
BigDecimal.valueOf(2000000000000000000L).multiply(
BigDecimal.valueOf(testValue))));
After using the BigDecimal, the multiplication returns the correct result which is
使用 BigDecimal 后,乘法返回正确的结果,即
80000000000000000000000000000000000
80000000000000000000000000000000000
回答by Honghe.Wu
(int)Long.valueOf("2345678901").longValue();