Question

Not sure if this is possible, but for my app I would like to get a locale based string that describes a number.

For example, if I had the number 10,000,000.

In english, I would expect the phrase "Ten Million". However, in Hindi, it would be One crore. Is there any properties in NSNumberFormatter, or NSLocale that could help me with this?

I have checked the docs (NSNumberFormatter, NSLocale), and havent found what I'm looking for yet. Obviously I could write some code to handle these two cases, but I'd like a way that could work for any locale.

Edit: Thanks to leo for the answer! Here is a small snippet of code that will get anyone looking for the same thing started:

NSNumberFormatter formatter = [[NSNumberFormatter alloc] init];
[self.formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"hi_hi"];
[self.formatter setLocale:locale];
NSNumber * myNumber = [NSNumber numberWithInt:10000];
self.numberLabel.text = [self.formatter stringFromNumber:myNumber];
Était-ce utile?

La solution

What you are looking for is the NSNumberFormatterSpellOutStyle style.

NSString* spelledOutString = [NSNumberFormatter localizedStringFromNumber:@10000000 numberStyle:NSNumberFormatterSpellOutStyle];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top