how to display arabic numbers inside iPhone apps without localizing each digit

只愿长相守 提交于 2019-11-29 14:18:54

问题


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


回答1:


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];



回答2:


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



来源:https://stackoverflow.com/questions/11670330/how-to-display-arabic-numbers-inside-iphone-apps-without-localizing-each-digit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!