views:

13473

answers:

4

Hello,

How can I convert an NSString containing a number of any primitive data type (e.g. int, float, char, unsigned int, etc.)? The problem is, I don't know which number type the string will contains at runtime.

I have an idea how to do it, but I'm not sure if this works with any type, also unsigned and floating point values:

long long scannedNumber;
NSScanner *scanner = [NSScanner scannerWithString:aString];
[scanner scanLongLong:&scannedNumber]; 
NSNumber number = [NSNumber numberWithLongLong: scannedNumber];

Thanks for help.

+4  A: 

You can use -[NSString integerValue], -[NSString floatValue], etc. However, the correct (locale-sensitive, etc.) way to do this is to use -[NSNumberFormatter numberFromString:] which will give you an NSNumber converted from the appropriate locale and given the settings of the NSNumberFormatter (including whether it will allow floating point values).

Barry Wark
+41  A: 

Use an NSNumberFormatter:

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:@"42"];
[f release];

If the string is not a valid number, then myNumber will be nil. If it is a valid number, then you now have all of the NSNumber goodness to figure out what kind of number it actually is.

Dave DeLong
Thank you, that is exactly what I need.
Enyra
plus points for using the meaning of life as the example.
James Hall
@James using any other number would've simply been incorrect.
Dave DeLong
A: 

Here's how I did it because I'm pretty sure that I'll only have strings that are ints, e.g., @"123", @"34839", etc.

[NSNumber numberWithInt:[[result valueForKey:@"id"] intValue]];

where [result valueForKey:@"id"] returns an NSString like @"2355342". So, if you're working with an hard-coded string, you'd do:

[NSNumber numberWithInt:[@"34534" intValue]];

Note: This is not very flexible. I'm not sure it will work with strings like: @"23 ft", @"34.45", etc.

MattDiPasquale
+1  A: 

Here's a working sample of NSNumberFormatter reading localized number NSString (xCode 3.2.4, osX 10.6), to save others the hours I've just spent messing around. Beware: while it can handle trailing blanks ("8,765.4 " works), this cannot handle leading white space and this cannot handle stray text characters. (Bad input strings: " 8" and "8q" and "8 q".)

NSString *tempStr = @"8,765.4";  
     // localization allows other thousands separators, also.
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]]; // happen by default?
[myNumFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
     // next line is very important!
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; // crucial

NSNumber *tempNum = [myNumFormatter numberFromString:tempStr];
NSLog(@"string '%@' gives NSNumber '%@' with intValue '%i'", 
    tempStr, tempNum, [tempNum intValue]);
[myNumFormatter release];  // good citizen
miker