0

I need to get an integer value from a written-out number, for example, get '3' in an int from the string @"three". How could I do this?

jscs
  • 63,694
  • 13
  • 151
  • 195
Phillip
  • 1,205
  • 3
  • 15
  • 22

1 Answers1

2

You could do this:

NSString * threeString = @"three";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
formatter.locale = [NSLocale currentLocale];
NSNumber * stringNumb = [formatter numberFromString:threeString];
int threeInt = [stringNumb intValue];
NSLog(@"ThreeInt: %i", threeInt);
Logan
  • 52,262
  • 20
  • 99
  • 128