Sj.
Sj.

Reputation: 592

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!

Upvotes: 1

Views: 2576

Answers (4)

user227416
user227416

Reputation: 11

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.

Upvotes: 1

Ian Henry
Ian Henry

Reputation: 22403

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]);

Upvotes: 2

Ben S
Ben S

Reputation: 69342

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.

Upvotes: 1

cobbal
cobbal

Reputation: 70733

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]);

Upvotes: 3

Related Questions