Locate float and specify the number of decimal places in iOS

I have a float that I would like to map to one decimal place. For instance. 104.8135674 ... will display as 104.8 in English. I usually use:

myString = [NSString stringWithFormat:@"%.1f",myFloat];

However, I would like to localize the number, so I tried:

myString = [NSString localizedStringWithFormat:@"%.1f",myFloat];

This works to assign the correct decimal character (e.g.

Russian: 104.8

German: 104.8

However, for the languages ​​used, do not use Arabic numbers (0123456789), the correct decimal character is used, but the numbers still remain in Arabic numbers. eg

Bahrain-Arabic: 104.8 (it should use different characters for numbers)

So I tried:

myString = [NSNumberFormatter localizedStringFromNumber:[NSNumber numberWithFloat:myFloat] numberStyle:kCFNumberFormatterDecimalStyle];

But with this I can not indicate the number of decimal places. He gives, for example,

Russian: 104.813

+5
1

NSNumberFormatter -setMaximumFractionDigits: , , :

NSNumberFormatter * formatter =  [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:1];
[formatter setLocale:[NSLocale currentLocale]];
NSString * myString = [formatter stringFromNumber:[NSNumber numberWithFloat:123.456]];
+11

All Articles