Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert English numbers to Arabic [duplicate]

Tags:

Possible Duplicate:
Convert String to another locale in java

I want to convert a java String that contains english numbers to arabic one's so i make this

int  arabic_zero_unicode= 1632; String str = "13240453"; StringBuilder builder = new StringBuilder();  for(int i =0; i < str.length(); ++i ) {     builder.append((char)((int)str.charAt(i) - 48+arabic_zero_unicode)); }  System.out.println("Number in English : "+str); System.out.println("Number In Arabic : "+builder.toString() ); 

the out put

Number in English : 13240453 Number In Arabic : ١٣٢٤٠٤٥٣ 

is there another more efficient way to do this ?

like image 606
confucius Avatar asked Jul 13 '12 10:07

confucius


People also ask

How do I change English numbers to Arabic numbers?

On the File tab, choose Options > Language. In the Set the Office Language Preferences dialog box, in the Editing Language list, choose the Arabic dialect you want, and then choose Add.

Are Arabic numbers the same as English?

Thus, reading numbers in Arabic is no different from English. This should give you one less thing to worry about when managing your Arabic source text.


1 Answers

This gives a 5x speedup over your version with a string of length 3036. This also checks to make sure you're only changing digits. It's about a 6x speedup without the if/else check.

Please pardon me if the characters are incorrect/misplaced. I had to find some of them from another source.

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() ); 
like image 109
Zéychin Avatar answered Sep 19 '22 18:09

Zéychin