RYN RYN - 1 year ago 89
Java Question

Convert String to another locale in java


I need to convert Arabic/Persian Numbers to it's English equal (for example convert "۲" to "2")

How can I do this?


Answer Source

I suggest you have a ten digit lookup String and replace all the digits one at a time.

public static void main(String... args) {

private static final String arabic = "\u06f0\u06f1\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9";
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);



The reason for using the strings as a lookup is that other characters such as . - , would be left as is. In fact a decimal number would be unchanged.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download