How to convert an NSString into an NSNumber
How can I convert a 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 contain 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 the help.
Solution 1:
Use an NSNumberFormatter
:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *myNumber = [f numberFromString:@"42"];
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.
Solution 2:
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).
Solution 3:
Objective-C
(Note: this method doesn't play nice with difference locales, but is slightly faster than a NSNumberFormatter
)
NSNumber *num1 = @([@"42" intValue]);
NSNumber *num2 = @([@"42.42" floatValue]);
Swift
Simple but dirty way
// Swift 1.2
if let intValue = "42".toInt() {
let number1 = NSNumber(integer:intValue)
}
// Swift 2.0
let number2 = Int("42')
// Swift 3.0
NSDecimalNumber(string: "42.42")
// Using NSNumber
let number3 = NSNumber(float:("42.42" as NSString).floatValue)
The extension-way This is better, really, because it'll play nicely with locales and decimals.
extension String {
var numberValue:NSNumber? {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter.number(from: self)
}
}
Now you can simply do:
let someFloat = "42.42".numberValue
let someInt = "42".numberValue
Solution 4:
For strings starting with integers, e.g., @"123"
, @"456 ft"
, @"7.89"
, etc., use -[NSString integerValue]
.
So, @([@"12.8 lbs" integerValue])
is like doing [NSNumber numberWithInteger:12]
.
Solution 5:
You can also do this:
NSNumber *number = @([dictionary[@"id"] intValue]]);
Have fun!