Is it possible to specify numbers in words using a separator?

I know NSNumberFormatter NSNumberFormatterSpellOutStyle, but it returns incorrectly formatted numbers: 932 returns "nine hundred thirty two" and not "nine hundred thirty two".

In another question, Dave DeLong said that he got "nine hundred thirty two" for the same code as what I want - the word "and" (or whatever, when they are localized) is used to separate large numbers . Is it possible?

0
source share
1 answer

If you just want to add β€œand”, I think you have to do it manually (either the behavior has changed since the answer you referred to was, or it was a typo in the answer). I think you could do it this way, but I don't know if this will work in all situations in which you want:

int num = 932; int leftNum = floor(num/100) * 100; int rightNum = num % 100; NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterSpellOutStyle]; NSString *combo; if (num > 100) { NSString *l = [f stringFromNumber:[NSNumber numberWithInt:leftNum]]; NSString *r = [f stringFromNumber:[NSNumber numberWithInt:rightNum]]; combo = [NSString stringWithFormat:@"%@ and %@",l,r]; }else{ combo = [f stringFromNumber:[NSNumber numberWithInt:num]]; } NSLog(@"%@", combo); 
+3
source

All Articles