Mohammad Khir Alsaid Mohammad Khir Alsaid - 3 months ago 108
Swift Question

convert arabic String to english number in Swift

How to convert non English number to English number like :
"٣٤٨٦٩١٢٨٨١" to "3486912881" in Swift
or I want to accept just english numbers and obtain others.

in Java Android this code is working for me :

private static String arabicToDecimal(String number) {
char[] chars = new char[number.length()];
for(int i=0;i<number.length();i++) {
char ch = number.charAt(i);
if (ch >= 0x0660 && ch <= 0x0669)
ch -= 0x0660 - '0';
else if (ch >= 0x06f0 && ch <= 0x06F9)
ch -= 0x06f0 - '0';
chars[i] = ch;
}
return new String(chars);
}

Answer

do like

    let NumberStr: String = "٣٤٨٦٩١٢٨٨١"
    let Formatter: NSNumberFormatter = NSNumberFormatter()
    Formatter.locale = NSLocale(localeIdentifier: "EN")
    let final = Formatter.numberFromString(NumberStr)  
    if final != 0 {
        print("\(final!)")
    }

output

enter image description here