2

Possible Duplicate:
How to convert an NSString into an NSNumber

How can I convert an NSString (@"Five" for example) into an NSNumber (5). I have looked to see if any questions answer this on Stack Overflow, but it seems that many people have been asking for a number to string conversion - not the other way around.

Community
  • 1
  • 1
James Anderson
  • 556
  • 3
  • 16
  • 41
  • are u trying to convert String "Five" into 5 (Number). Or String "5" into 5. – Satish Azad Jan 29 '13 at 19:45
  • 2
    Well he did say the former… – Jonathan Grynspan Jan 29 '13 at 19:47
  • see http://stackoverflow.com/questions/1448804/how-to-convert-an-nsstring-into-an-nsnumber – Rachel Gallen Jan 29 '13 at 19:50
  • You are looking for something like NSDate's dateWithNaturalLanguageString: for numbers. Or something like the inverse of NSNumberFormatter's parse with NSNumberFormatterSpellOutStyle. Or a NSLinguisticTagger with NSLinguisticTagNumber able to parse complex numbers. Or a Levenshtein distance algorithm with a database of common numbers. But there is not such a thing *sad face*. – Jano Jan 29 '13 at 19:58

3 Answers3

3

There's no built-in functionality for this for a few reasons:

1) It's fuzzy. To say 2013 (the year), I might say "two thousand thirteen", "two thousand and thirteen", "twenty thirteen", "two kay thirteen", etc. That's not even taking into account dialectical differences.

2) It's not easily localized. The rules for speaking aloud a number vary widely between languages (so a single set of rules can't really be used for every locale), and Apple only provides APIs that can be used around the world. Most people on this planet don't speak English.

3) It assumes that the user can spell correctly. "Fourty" is not a number. "Forty" is.

If you tell us why you're trying to do this (i.e. what the goal is), maybe we can suggest a better approach?

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
3

This works roughly and should at least get you started:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;

NSArray *testStrings = @[@"Five",@"twenty-four",@"Nineteen",@"One hundred forty-two"];

for (NSString *test in testStrings) {

    NSNumber *result = [formatter numberFromString:[test lowercaseString]];

    NSLog (@"String %@ -> %@ as number",test,result);

}

Some things to think about:

  • You'll want to set the locale correctly to either some fixed locale (if these strings are all coming from a server or something in one constant language) or let it float to the user's locale (if the user is the one entering the strings)
  • As others have pointed out, this parser may not be sophisticated enough for your needs. For example, the last string "thirty three" actually returns the wrong result. The others work correctly. As @JonathanGrynspan suggested, if you tell us more about your goal here, someone can maybe suggest something better.

I hope that helps.

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
-1
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:@"42"];

Hope this helps..

lakshmen
  • 28,346
  • 66
  • 178
  • 276