Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 356 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert an NSString into an NSNumber

#1
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.
Reply

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

#3
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
Reply

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

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

Reply

#5
What about C's standard `atoi`?

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

Do you think there are any caveats?
Reply

#6
You can also do this:

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

Have fun!
Reply

#7
I think NSDecimalNumber will do it:

Example:

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

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

#8
NSDecimalNumber *myNumber = [NSDecimalNumber decimalNumberWithString:@"123.45"];
NSLog(@"My Number : %@",myNumber);
Reply

#9
I wanted to convert a string to a double. This above answer didn't quite work for me. But this did:

[To see links please register here]


All I pretty much did was:

double myDouble = [myString doubleValue];

Reply

#10
For strings starting with integers, e.g., `@"123"`, `@"456 ft"`, `@"7.89"`, etc., use [`-[NSString integerValue]`][1].

So, `@([@"12.8 lbs" integerValue])` is like doing `[NSNumber numberWithInteger:12]`.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through