问题
I have an english string that may or may not have numbers. But i want those numbers to be printed on screen as Persian numbers.
For example if NSString *foo = @"a string with numbers 1 2 3;"
then output should be a string with numbers ۱۲۳
The code I'm using right now is :
-(NSString *)convertEnNumberToFarsi:(NSString *) number{
NSString *text;
NSDecimalNumber *someNumber = [NSDecimalNumber decimalNumberWithString:number];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fa"];
[formatter setLocale:gbLocale];
text = [formatter stringFromNumber:someNumber];
return text;
}
This method only converts a string that is only in numbers, but as mentioned above i want to convert any string that may or may not have numbers in it.
How can i achieve this?
回答1:
The simple way:
NSDictionary *numbersDictionary = @{@"1" : @"۱", @"2" : @"۲", @"3" : @"۳", @"4" : @"۴", @"5" : @"۵", @"6" : @"۶", @"7" : @"۷", @"8" : @"۸", @"9" : @"۹"};
for (NSString *key in numbersDictionary) {
str = [str stringByReplacingOccurrencesOfString:key withString:numbersDictionary[key]];
}
Other solution more flexible with locale:
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
for (NSInteger i = 0; i < 10; i++) {
NSNumber *num = @(i);
str = [str stringByReplacingOccurrencesOfString:num.stringValue withString:[formatter stringFromNumber:num]];
}
Note: this code wrote without IDE, it can be with syntax errors.
回答2:
In swift 3:
func convertEngNumToPersianNum(num: String)->String{
let number = NSNumber(value: Int(num)!)
let format = NumberFormatter()
format.locale = Locale(identifier: "fa_IR")
let faNumber = format.string(from: number)
return faNumber!
}
Check before force unwrap to prevent crash.
回答3:
Swift 3:
However other answers are correct, there is a little problem in converting String
to Int
.
Converting a String
to Int
removes left zeros which is not good (Specially in converting cell phones and national Ids). To avoid this problem I prefer replacing each English number to Persian.
static func convertToPersian(text : String)-> String {
let numbersDictionary : Dictionary = ["0" : "۰","1" : "۱", "2" : "۲", "3" : "۳", "4" : "۴", "5" : "۵", "6" : "۶", "7" : "۷", "8" : "۸", "9" : "۹"]
var str : String = text
for (key,value) in numbersDictionary {
str = str.replacingOccurrences(of: key, with: value)
}
return str
}
This method can also be used to replace numbers within text and its not necessary to have a pure number to convert to Persian.
回答4:
I'm afraid there is no other way than replacing them.
e.g.:
str =[str stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
.
.
.
回答5:
+(NSString *)convertNumToPersian:(NSString *)EnglishNumString
{
if ( [EnglishNumString isEqual:[NSNull null]] )
return @"";
NSString *myString = [EnglishNumString stringByReplacingOccurrencesOfString:@"1" withString:@"۱"];
myString = [myString stringByReplacingOccurrencesOfString:@"2" withString:@"۲"];
myString =[myString stringByReplacingOccurrencesOfString:@"3" withString:@"۳"];
myString =[myString stringByReplacingOccurrencesOfString:@"4" withString:@"۴"];
myString =[myString stringByReplacingOccurrencesOfString:@"5" withString:@"۵"];
myString =[myString stringByReplacingOccurrencesOfString:@"6" withString:@"۶"];
myString =[myString stringByReplacingOccurrencesOfString:@"7" withString:@"۷"];
myString =[myString stringByReplacingOccurrencesOfString:@"8" withString:@"۸"];
myString =[myString stringByReplacingOccurrencesOfString:@"9" withString:@"۹"];
myString =[myString stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
return myString;
}
回答6:
/** * In this function convert Arabic number to English number. * @sample stringValue : Pass String value. */
fun numberArabicToEnglish(stringValue: String): String {
val value = stringValue
.replace("١", "1")
.replace("٢", "2")
.replace("٣", "3")
.replace("٤", "4")
.replace("٥", "5")
.replace("٦", "6")
.replace("٧", "7")
.replace("٨", "8")
.replace("٩", "9")
.replace("٠", "0")
return value
}`enter code here`
回答7:
convert number to any locale
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "ar")
if let localized = formatter.string(from: NSNumber(value: comp.0)) {
return "\(localized)"
}
example:
(lldb) po formatter.string(from: NSNumber(value: 0)) ▿ Optional - some : "٠"
(lldb) po formatter.string(from: NSNumber(value: 1)) ▿ Optional - some : "١"
(lldb) po formatter.string(from: NSNumber(value: 2)) ▿ Optional - some : "٢"
(lldb) po formatter.string(from: NSNumber(value: 3)) ▿ Optional - some : "٣"
(lldb) po formatter.string(from: NSNumber(value: 4)) ▿ Optional - some : "٤"
(lldb) po formatter.string(from: NSNumber(value: 5)) ▿ Optional - some : "٥"
(lldb) po formatter.string(from: NSNumber(value: 6)) ▿ Optional - some : "٦"
(lldb) po formatter.string(from: NSNumber(value: 7)) ▿ Optional - some : "٧"
(lldb) po formatter.string(from: NSNumber(value: 8)) ▿ Optional - some : "٨"
(lldb) po formatter.string(from: NSNumber(value: 9)) ▿ Optional - some : "٩"
(lldb) po formatter.string(from: NSNumber(value: 10)) ▿ Optional - some : "١٠"
来源:https://stackoverflow.com/questions/30479225/how-to-convert-english-numbers-inside-a-string-to-persian-arabic-numbers-in-obje