Cocos2d iphone 字符串长度的未知整数转换>;10英寸cocos2d/Xcode应用程序(iOS 7.0)
我正在使用Cocos2d iphone 字符串长度的未知整数转换>;10英寸cocos2d/Xcode应用程序(iOS 7.0),cocos2d-iphone,ios7,integer,Cocos2d Iphone,Ios7,Integer,我正在使用cocos2d2.0和iOS7.0。在尝试获取长度较大(通常大于10)的字符串的整数值或浮点值时,我总是得到一些未知的输出,如下所示: 当字符串长度为10时,即: NSString *amount = @"12345678901"; -- added a '1' after the string, so length = 11 NSLog(@"AmountStr=|%@|",amount); NSLog(@"Amount =|%d|",[amount int
cocos2d2.0
和iOS7.0。在尝试获取长度较大(通常大于10)的字符串的整数值或浮点值时,我总是得到一些未知的输出,如下所示:
当字符串长度为10时,即:
NSString *amount = @"12345678901"; -- added a '1' after the string, so length = 11
NSLog(@"AmountStr=|%@|",amount);
NSLog(@"Amount =|%d|",[amount integerValue]);
然后我得到的输出是:
AmountStr=|12345678901| -- This is correct
Amount =|2147483647| -- But what about this..!!! :O
我尝试了integerValue
、intValue
、和floatValue
。每次都会发生相同的错误。那么如何查找长度大于10的字符串的int值呢。请帮帮我
NSLog(@"Amount =|%lli|",[amount longLongValue]);
您试图将一个数字打印为大于整数所能容纳的最大数字的整数。这甚至不是数字的多少。尝试使用30000000000
执行此操作将复制“错误”
对于NSString
,还有doubleValue
方法,它将为您提供比floatValue
更多的有效数字
此外,使用%d
调用integerValue
甚至可以工作,这让我有点惊讶intValue
返回一个int
。但是integerValue
返回一个NSInteger
。通常,在将格式说明符与NSInteger
一起使用时,需要使用%ld
并将NSInteger
强制转换为长
对于最多38位数字,您可以始终使用NSDecimalNumber
NSDecimalNumber *myNum = [NSDecimalNumber decimalNumberWithString:amount];
NSLog(@"%@", [myNum descriptionWithLocale:[NSLocale systemLocale]]);
这个问题称为整数溢出。。。
NSLog(@"Amount =|%lli|",[amount longLongValue]);
NSDecimalNumber *myNum = [NSDecimalNumber decimalNumberWithString:amount];
NSLog(@"%@", [myNum descriptionWithLocale:[NSLocale systemLocale]]);