ChatGPT解决这个技术问题 Extra ChatGPT

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.


D
Dave DeLong

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.


For people where it doesn't seem to work: Check if it's related to your locale. The NSNumberFormatter (as far as I know) by default uses the US locale, i.e. expects the decimal separator to be the "." character. If you use "," to separate the fraction, you may need to tell the formatter to use your current locale: [f setLocale:[NSLocale currentLocale]];
@pille by default the locale is the +currentLocale, but you're correct; one must always consider the locale when dealing with converting stuff to-and-from human-readable form.
Where did you find that it was currentLocale by default? I can confirm that numberFromString is using dot notation even on a French phone where comma is used. I can confirm that by default, when you output a number, it uses the current locale by default
But what if the string can contain either int or float?
D
Dave DeLong

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).


+q Depending on the situation, non-locale-sensitive might actually be the correct way.
I had to convert @"2000" to an int and [@"2000" integerValue] worked nicely and is a little simpler for my case.
There are also huge performance differences among these methods.
this does not work with ARC: it won't convert NSInteger to NSNumber. I have to further use [NSNumber numberWithInteger ...]
What @Thilo said. "Correct" and "locale-sensitive" are not synonyms. The correct thing to do actually depends upon how the number got into the string in the first place. If you read it from user input, then local-sensitive is what you want. From any other source (i.e. you put it there programmatically, you're consuming it as part of the response from some external API, etc.) local-sensitive is not appropriate and could even give incorrect results.
K
Kevin R

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

I prefer this based on profiling when I was parsing a large amount of string values. Using this syntax rather then an NSNumberFormatter led to significant reduction in time spent parsing the string to NSNumber. And yes the NSNumberFormatter was cached and reused.
this literal syntax didn't exist when this question was asked. I'd say this is the correct answer nowadays though.
I agree, but do note that some other countries use the , and . the other way around (eg 42.000,42. Something floatValue probably does not account for?
This is the nicest solution on here, however I have just run a test and you will lose unsigned precision so WATCH OUT!!!
In my use case, I needed to get an NSNumber dictionary key from an NSString, so @([@"42" intValue]) worked perfectly. Not worried about Locale.
m
ma11hew28

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].


M
MacMark

You can also do this:

NSNumber *number = @([dictionary[@"id"] intValue]]);

Have fun!


A
Ad1905

If you know that you receive integers, you could use:

NSString* val = @"12";
[NSNumber numberWithInt:[val intValue]];

m
miker

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

this doesn't work for me. I'm developing for 5.0 with xcode 4.3.2. any ideas why?
C
Community

I wanted to convert a string to a double. This above answer didn't quite work for me. But this did: How to do string conversions in Objective-C?

All I pretty much did was:

double myDouble = [myString doubleValue];

This doesn't convert the NSInteger to a NSNumber. It converts it to a double.
P
P.J.Radadiya

Thanks All! I am combined feedback and finally manage to convert from text input ( string ) to Integer. Plus it could tell me whether the input is integer :)

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:thresholdInput.text];

int minThreshold = [myNumber intValue]; 

NSLog(@"Setting for minThreshold %i", minThreshold);

if ((int)minThreshold < 1 )
{
    NSLog(@"Not a number");
}
else
{
    NSLog(@"Setting for integer minThreshold %i", minThreshold);
}
[f release];

J
JeanNicolas

I think NSDecimalNumber will do it:

Example:

NSNumber *theNumber = [NSDecimalNumber decimalNumberWithString:[stringVariable text]]];

NSDecimalNumber is a subclass of NSNumber, so implicit casting allowed.


m
martin

What about C's standard atoi?

int num = atoi([scannedNumber cStringUsingEncoding:NSUTF8StringEncoding]);

Do you think there are any caveats?


have you read the question? you're to provide an NSNumber instance (an Objective-C object) not an int primitive.
h
hallucinations

You can just use [string intValue] or [string floatValue] or [string doubleValue] etc

You can also use NSNumberFormatter class:


P
Parv Bhasker

you can also do like this code 8.3.3 ios 10.3 support

[NSNumber numberWithInt:[@"put your string here" intValue]]

S
Sandip Patel - SM
NSDecimalNumber *myNumber = [NSDecimalNumber decimalNumberWithString:@"123.45"];
NSLog(@"My Number : %@",myNumber);

M
Maple

Try this

NSNumber *yourNumber = [NSNumber numberWithLongLong:[yourString longLongValue]];

Note - I have used longLongValue as per my requirement. You can also use integerValue, longValue, or any other format depending upon your requirement.


u
user431791

Worked in Swift 3

NSDecimalNumber(string: "Your string") 

A
Ashu

I know this is very late but below code is working for me.

Try this code

NSNumber *number = @([dictionary[@"keyValue"] intValue]]);

This may help you. Thanks


g
garg
extension String {

    var numberValue:NSNumber? {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        return formatter.number(from: self)
    }
}

let someFloat = "12.34".numberValue