开发者

NSString - doubleValue is returning NaN from a viable NSString

Ok, so I am pulling an NSString from and NSMutableArray and storing it in an NSString:

entry = [NSString stringWithString:[array objectAtIndex:row]];

This is wrapped inside of a couple different for-loops. I then convert it to a doubleValue and store it in a number but apparently [entry doubleValue] only returns a NaN instead of 开发者_开发技巧a number. I threw in an NSLog for 'entry' and '[entry doubleValue]'. EVERY time I run this I get the same results:

entry: 22.414 value: nan

Any ideas? thanks!


you might check to be sure that you don't have spaces or nonprinting characters in entry

Easiest way to do this:

NSLog(@"%@", [entry dataUsingEncoding:NSUTF8StringEncoding]);


Yep, I'd say it's probably some non-printing characters. This works fine for me:

NSString *test = @"22.414";
NSLog(@"doubleValue: %f", [test doubleValue]);


If you're certain there aren't any other characters in the strings, have a look at the NSScanner class. It usually does a better job at reading strings.


It may be, that you have set a system language where the locale says you are using for example comma instead of a dot as decimal seperator. I don't know if this happens with double value, but I had issues with NSNumberFormater.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜