numeric values within NSString are displayed in a wrong locale

I am parsing an NSString from JSON and then the value is displayed in a UILable, the value contains numbers like @"12" , @"13"

Now when I do this:

self.lable.text =  @"12";

the label display the value in arabic numbers ( ١٢ )

I have tried to parse the JSON response as NSNumber then use NSNumberFormatter to force the "en_US" locale in the below code segment but it didn't work.

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:0];
[numberFormatter setMinimumFractionDigits:0];
NSString *formattedNumberString = [numberFormatter stringFromNumber:number];
self.lable.text = formattedNumberString;

My app language is "English - development language" which is listed under the info tab in the project section.

The region of the device I am running the app on is set to "United States" and the language is set to "English"

I have tried to run [NSLocale currentLocale] to double check but I got "en_US"

I am stuck with this issue, any pointers and help is appreciated.


That's odd. I would not force the local but setting it to the local setting of the device and you could set the string format as follows:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = [NSLocale currentLocale];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
numberFormatter.maximumFractionDigits = 0;
self.label.text = [NSString StringWithFormat:@"%@", [numberFormatter stringForObjectValue:number]];

another suggestion is always to use localizedStringFromNumber to ensure the right local behavior of the string:

NSString *formattedNumberString = [numberFormatter localizedStringFromNumber:number];

I hope it helps.

链接地址: http://www.djcxy.com/p/67362.html

上一篇: 如何处理Spring MVC中的静态内容?

下一篇: NSString中的数字值显示在错误的语言环境中