Domanda

I'm working on an android application in persian language that gets a number from an EditText input. the problem is that I'm using a custom typeface for the EditText and when I try to get the number in it and convert it to double by using Double.ValueOf method I get a NumberFormatException.When I get the value of the EditText as an string in LogCat it is the number with farsi characters. An strange thing is when I insert a double number within the code in the EditText this problem occurs and when the user puts it's number everything is OK! Any ideas for solving this problem? Is there any way to convert the string with farsi number characters to double? Thank you for your help.

È stato utile?

Soluzione

you can use this code to convert your persian numbers to standard numbers:

char[] arabicChars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
int  arabic_zero_unicode= 1632;
String str = "١٣٢٤٠٤٥٣";
StringBuilder builder = new StringBuilder();
boolean charFound=false;
for(int i =0; i < str.length(); ++i ) {
    charFound=false;
    for(int j=0;j<arabicChars.length;j++){
        if(str.charAt(i)==arabicChars[j]){
            builder.append((char)((int)str.charAt(i) + 48-arabic_zero_unicode));
            charFound=true;
        }
        if(!charFound)
            builder.append(str.charAt(i));

    }

}

System.out.println("Number in English : "+builder.toString());

now you can use

Double.ValueOf(builder.toString());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top