How can I convert English numbers inside a string to Persian / Arabic numbers in Objective-C?

I have an English string that may or may not contain a number. But I want these numbers to be printed on the screen as Persian numbers.

For example, if NSString *foo = @"a string with numbers 1 2 3;"

then the output should be a string with numbers ۱۲۳

The code I'm using 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 a number in it.

How can I achieve this?

+5


source to share


6 answers


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

      

Another solution is 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 was written without an IDE, it may contain syntax errors.

+8


source


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 unloading force to prevent failure.

+8


source


Swift 3:

However, the other answers are correct, there is a small problem when converting String

to Int

.

Converting a String

to Int

removes left zeros which are not good (especially when converting cell phones and national IDs). To avoid this problem, I prefer to replace every English number with 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 technique can also be used to replace numbers in text, rather than having a pure number to convert to Persian.

+4


source


I am afraid there is no other way than to replace them.

eg:.

str =[str stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
.
.
.

      

+1


source


+(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;
}

      

0


source


/ ** * In this function, convert an Arabic number to an English number. * @sample stringValue: pass a 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'

      

0


source







All Articles