阿拉伯数字到英文数字android

时间:2014-04-01 06:49:24

标签: android

我正在使用此代码将英文数字翻译成阿拉伯语。但现在我想把阿拉伯数字改成英文数字。

private void decimalToArabic() {
    String str = showOutputEdit.getText().toString();
    char[] arabicChars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
    //  char[] arabicChars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
        StringBuilder builder = new StringBuilder();
        for(int i =0;i<str.length();i++)
        { 
            if(Character.isDigit(str.charAt(i)))
            {
                builder.append(arabicChars[(int)(str.charAt(i))-48]);
            }
            else
            {
                builder.append(str.charAt(i));
            }
        }
        System.out.println("Number in English : "+str);
        System.out.println("Number In Arabic : "+builder.toString() );
        showOutputEdit.setText(builder.reverse().toString());
}

我需要改变什么?

4 个答案:

答案 0 :(得分:4)

我认为你可以使用这样的东西。(这里我将整数转换为阿拉伯语)

 public String convertToArabic(int value)
{
    String newValue =   (((((((((((value+"").replaceAll("1", "١")).replaceAll("2", "٢")).replaceAll("3", "٣")).replaceAll("4", "٤")).replaceAll("5", "٥")).replaceAll("6", "٦")).replaceAll("7", "٧")).replaceAll("8", "٨")).replaceAll("9", "٩")).replaceAll("0", "٠"));       
    return newValue;
}

答案 1 :(得分:3)

private static String arabicToenglish(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);
}

答案 2 :(得分:2)

阿拉伯语字符数组必须颠倒

<强> WRONG

char[] chars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};

从右

char[] chars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};

这只是因为stackoverflow编辑器,但是如果你要复制文本并粘贴(CTRL + C CTRL + V)它会正常工作。

所以函数看起来像这样

private static String toArabicDigits(String eng) {

    char[] chars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < eng.length(); ++i) {
        if (Character.isDigit(eng.charAt(i))) {
            builder.append(chars[(int)(eng.charAt(i))-48]);
            System.out.println("char - " + eng.charAt(i) + " " + (int)(eng.charAt(i)-48) + " - " + chars[(int)(eng.charAt(i))-48]);
        } else {
            builder.append(eng.charAt(i));
        }
    }

    return builder.toString();
}

答案 3 :(得分:0)

public String convertArabic(String arabicStr) {
  char[] chArr = arabicStr.toCharArray();
  StringBuilder sb = new StringBuilder();
  for (char ch : chArr) {
    if (Character.isDigit(ch)) {
      sb.append(Character.getNumericValue(ch));
    } else {
      sb.append(ch);
    }
  }
  return sb.toString();
}