7

I want to know how to display arabic numbers inside iPhone apps without localizing each digit.

like for example, I have an integer in the code whose value = 123

I want to display it as ١٢٣ without having to localize each digit on its own and force the app to use arabic as the localization language regardless of the user's chosen language

because I have many numbers all over the application that change dynamically, so I need a generic smart solution

Evronia
  • 75
  • 1
  • 4
  • 2
    You mean "Eastern Arabic numerals", as opposed to "Western Arabic numerals". –  Jul 26 '12 at 13:29

2 Answers2

22
NSDecimalNumber *someNumber = [NSDecimalNumber decimalNumberWithString:@"123"];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSLocale *arLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"ar"] autorelease];
[formatter setLocale:arLocale];
NSLog(@"%@", [formatter stringFromNumber:someNumber]); // Prints in Arabic
[formatter setLocale:[NSLocale currentLocale]];
NSLog(@"%@", [formatter stringFromNumber:someNumber]); // Prints in Current Locale
[formatter release];
Stonz2
  • 6,306
  • 4
  • 44
  • 64
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
1

In case you have floating point numbers you must add this line after initialing NSNumberFormatter [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; therefore the modification of above code would as follow

NSString *test = [NSString stringWithFormat:@"%lu", fileSizeEvet];
//    NSString *test = [NSString stringWithFormat:@"%f", (double)folderSize/1024/2014];
NSDecimalNumber *someNumber = [NSDecimalNumber decimalNumberWithString:test];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"ar"];

[formatter setLocale:gbLocale];
float myInt = [someNumber floatValue]/1024/1024;
//    NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:myInt]]);
//    NSLog(@"%@", [formatter stringFromNumber:someNumber]); // Prints in Arabic
NSString *folderSizeInArabic = [formatter stringFromNumber:[NSNumber numberWithFloat:myInt]];
fileSizeLabel.text = [NSString stringWithFormat:@" قه‌باره‌ی فایل: %@ مب", folderSizeInArabic];

For more info visit this link

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
Aree Ali
  • 55
  • 1
  • 1
  • 6