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?


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

public static void main(String... args) {
    System.out.println(arabicToDecimal("۴۲"));
}
//used in Persian apps
private static final String extendedArabic = "\u06f0\u06f1\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9";

//used in Arabic apps
private static final String arabic = "\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669";

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

prints

42

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.


I achived this by java.math.BigDecimal Class, Below is the code snippet

String arabicNumerals = "۴۲۴۲.۴۲";
String englishNumerals = new BigDecimal(arabic).toString();
System.out.println("Number In Arabic : "+arabicNumerals);
System.out.println("Number In English : "+englishNumerals);

Result

Number In Arabic : ۴۲۴۲.۴۲
Number In English : 4242.42

NB : The Above code will not work if there is any charactors other than numeric digits in arabicNumerals, For Eg : ۴,۲۴۲.۴۲ will result in java.lang.NumberFormatException, so you may remove other charactors using Character.isDigit(char ch) in another logic and use the above code. All normal cases are working charmly !!

Good Day


I found a simpler and faster way which includes the two arabic code pages too.

public static String convertToEnglishDigits(String value)
{
     String newValue = value.replace("١", "1").replace("٢", "2").replace("٣", "3").replace("٤", "4").replace("٥", "5")
             .replace("٦", "6").replace("7", "٧").replace("٨", "8").replace("٩", "9").replace("٠", "0")
             .replace("۱", "1").replace("۲", "2").replace("۳", "3").replace("۴", "4").replace("۵", "5")
             .replace("۶", "6").replace("۷", "7").replace("۸", "8").replace("۹", "9").replace("۰", "0");

     return newValue;
}

It will return the numbers in English format or vise versa if you change the replace from.
("۰", "0") to ("0","۰")


Try this guys:

/**
 * Utility class to detect arabic languages and convert numbers into arabic digits.
 *
 * @author Ahmed Shakil
 * @date 09-24-2012
 */
public final class ArabicUtil {

private static final char[] DIGITS = {'\u0660','\u0661','\u0662','\u0663','\u0664','\u0665','\u0666','\u0667','\u0668','\u0669'};

/**
 * Returns <code>true</code> if the provided language code uses arabic characters; othersise <code>false</code>.
 * @param lang ISO language code.
 * @return <code>true</code> if the provided language code uses arabic characters; othersise <code>false</code>
 */
public static boolean isArabic (String lang) {
    return "ar".equals(lang) || "fa".equals(lang) || "ur".equals(lang);
}

/**
 * Convert digits in the specified string to arabic digits.
 */
public static String convertDigits (String str) {
    if (str == null || str.length() == 0) return str;

    char[] s = new char[str.length()];
    for(int i =0;i<s.length;i++)
        s[i] = toDigit( str.charAt( i ) );

    return new String(s);
}

/**
 * Convert single digit in the specified string to arabic digit.
 */
public static char toDigit (char ch) {
    int n = Character.getNumericValue( (int)ch );
    return n >=0 && n < 10 ? ARABIC[n] : ch;
}

/**
 * Convert an int into arabic string.
 */
public static String toString (int num) {
    return convertDigits( Integer.toString( num ) );
}
}

BTW there is a difference between arabic digits vs. urdu/farsi: Arabic:

private static final char[] ARABIC     = {'\u0660', '\u0661', '\u0662', '\u0663', '\u0664', '\u0665', '\u0666', '\u0667', '\u0668', '\u0669'};

Urdu or Farsi:

private static final char[] URDU_FARSI = {'\u06f0', '\u06f1', '\u06f2', '\u06f3', '\u06f4', '\u06f5', '\u06f6', '\u06f7', '\u06f8', '\u06f9'};

First make it work, then make it look nice ;-)

public static char persianDigitToEnglish(char persianDigit) {
    return (char) (((int)persianDigit) - ((int)'۲' - (int)'2'));
}

Works for 2, unfortunately I don't know other Persian digits, could You give it a try?

assertThat(persianDigitToEnglish('۲')).isEqualTo('2');

EDIT: (based on Peter Lawrey String version, but uses StringBuilder)

public static String persianDigitToEnglish(String persianNumber) {
    StringBuilder chars = new StringBuilder(persianNumber.length());
    for (int i = 0; i < persianNumber.length(); i++)
        chars.append(persianDigitToEnglish(persianNumber.charAt(i)));
    return chars.toString();
}

private static char persianDigitToEnglish(char persianDigit) {
    return (char) (((int)persianDigit) - ((int)'۲' - (int)'2'));
}