1
votes

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.

1
"12" is not the NSString value for 12. @"12" is the NSString value for 12. "12" is a char* string value for 12, which would usually cause a crash when assigned to a text property, but which might occasionally result in apparently "normal" but bogus operation. - Hot Licks

1 Answers

1
votes

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.