-[NSString intValue]返回的值与字符串说的完全不同

时间:2021-09-07 06:57:10

I have a numeric string and have observed a discrepency where its intValue is vastly different from its string value. The code below:

我有一个数字字符串,并且观察到它的值与它的字符串值有很大的不同。下面的代码:

NSString *string = [result objectForKey:@"id"];
NSLog(@"ID %@",string);
NSLog(@"ID as integer %i",[string intValue]);

gives an output:

给出了输出:

"ID 100004378121454"
"ID as integer 2147483647"

The only logical guess I can make is that the string is too long to be converted to an int... In any case I tried longLongValue and such - to different results but not the one it should be.

我能做出的唯一合乎逻辑的猜测是,字符串太长,不能转换成int型……无论如何,我尝试了朗朗朗格沃斯(longlonglonglonglongvalue)之类的方法——以不同的结果,而不是应该是哪种结果。

1 个解决方案

#1


14  

Your number(100004378121454) is greater number than a simple int can handle, you have to use long long type in this case(to not get your number truncated to the int32 maximum value) :

您的编号(100004378121454)比一个简单的int类型所能处理的数大,在这种情况下,您必须使用长类型(以避免您的编号被截断为int32最大值):

NSString *string = @"100004378121454";
NSLog(@"ID %@",string);
NSLog(@"ID as long long %lli",[string longLongValue]); 

Output :

输出:

ID              100004378121454
ID as long long 100004378121454

#1


14  

Your number(100004378121454) is greater number than a simple int can handle, you have to use long long type in this case(to not get your number truncated to the int32 maximum value) :

您的编号(100004378121454)比一个简单的int类型所能处理的数大,在这种情况下,您必须使用长类型(以避免您的编号被截断为int32最大值):

NSString *string = @"100004378121454";
NSLog(@"ID %@",string);
NSLog(@"ID as long long %lli",[string longLongValue]); 

Output :

输出:

ID              100004378121454
ID as long long 100004378121454