1

I am using the following below java code to convert the numerics to arabic

    String str = "1234-5678-9101";

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

    for (int i = 0; i < str.length(); ++i) {
        if (Character.isDigit(str.charAt(i))) {
            builder.append(Chars[(int)(str.charAt(i))-48]);
        } else {
            builder.append(str.charAt(i));
        }
    }

the expected output is ٩١٠١-٥٦٧٨-١٢٣٤ but the result is ١٢٣٤-٥٦٧٨-٩١٠١ (reverse direction)

user2079954
  • 479
  • 3
  • 10
  • 21

2 Answers2

1

Thanks to everyone, this issue was resolved using the below code:

String str = "1234-5678-9101";

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

for (int i = 0; i < str.length(); ++i) {
    if (Character.isDigit(str.charAt(i))) {
        builder.append(Chars[(int)(str.charAt(i))-48]);
    } else {
        builder.append("\u202A");
        builder.append(str.charAt(i));
    }
}
user2079954
  • 479
  • 3
  • 10
  • 21
  • [This will be good reading for the future.](http://www.fileformat.info/info/unicode/char/202a/index.htm) Glad you got it to work. – Makoto Apr 15 '13 at 04:52
0

The code works, that's obvious. But it looks to me that the dash character (-) is breaking the rules of Arabic writing from right to left - it seems that each group of numbers is correct but the overall order is not.

expected: ٩١٠١-٥٦٧٨-١٢٣٤
actual: ١٢٣٤-٥٦٧٨-٩١٠١

I can't help much more than this because that's all I know on Arabic alphabet. Hope this helps.

Cebence
  • 2,406
  • 2
  • 19
  • 20
  • It's an answer that doesn't *quite* answer the question. It would probably be better served as a comment instead. – Makoto Apr 15 '13 at 04:53
  • Agreed. At that time I didn't know a comment can hold 600 characters and be styled. But it did help to solve the problem :) – Cebence Apr 16 '13 at 15:06