The number (secondsSinceUnixEpoch*1000)+120000
looks to me like it's going to be way too large to fit in an int
. Chances are the integer is overflowing and becoming negative.
Why is converting my float to an int making the number negative?
-
30-07-2022 - |
Вопрос
NSTimeInterval expirationTime = (secondsSinceUnixEpoch*1000)+120000;
expirationTime = ceil(expirationTime/2);
int expirationInt = (int)expirationTime;
NSLog(@"%d", expirationInt);
The log output is always negative, even though before I convert it to an int it's positive... I tried just multiplying it by -1 to make it positive again and it's just staying negative! I'm totally perplexed.... don't know much about C, am I just doing something silly??
Решение
Другие советы
Converting to long long
is one solution. As you stated in a comment, you want to show a whole number for use in a URL. Just do this:
NSTimeInterval expirationTime = (secondsSinceUnixEpoch*1000)+120000;
expirationTime = ceil(expirationTime/2);
NSString *urlString = [NSString stringWithFormat:@"http://example.com?time=%.0f", expirationTime];
This will format the decimal number as a whole number.
Не связан с StackOverflow