1000 * 60 * 60 * 24 * 30就产生负号 [英] 1000 * 60 * 60 * 24 * 30 results in a negative number
问题描述
我试图来计算30天乘以毫秒然而,结果不断地最终被一个负数days_30的价值,我不知道为什么。
I'm attempting to calculate 30 days by multiplying milliseconds however the result continually ends up being a negative number for the value of days_30 and I'm not sure why.
任何建议都大大AP preciated!
Any suggestions are greatly appreciated!
code片断:
// check to ensure proper time has elapsed
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("DataCountService", 0);
long days_30 = 1000 * 60 * 60 * 24 * 30;
long oldTime = pref.getLong("smstimestamp", 0);
long newTime = System.currentTimeMillis();
if(newTime - oldTime >= days_30){
在days_30值结果:-1702967296
days_30 value results in: -1702967296
P.S。
double days_30 = 1000 * 60 * 60 * 24 * 30;
double oldTime = pref.getLong("smstimestamp", 0);
double newTime = System.currentTimeMillis();
if(newTime - oldTime >= days_30){
结果在一个较小的 - 但仍然负数。 -1.702967296E9
Results in a smaller - but still negative number. -1.702967296E9
推荐答案
您成倍整数
在一起,溢出发生的原因的最大整数 2 ^ 31 - 1
。只有乘法后,它会转换到长
。先拿号作为长
。
You are multiplying ints
together, and overflow occurs because the maximum integer is 2^31 - 1
. Only after the multiplications does it get converted to a long
. Cast the first number as a long
.
long days_30 = (long) 1000 * 60 * 60 * 24 * 30;
或使用长
的文字:
long days_30 = 1000L * 60 * 60 * 24 * 30;
这将迫使长
的数学运算,从一开始。
That will force long
math operations from the start.
这篇关于1000 * 60 * 60 * 24 * 30就产生负号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!