Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Persian number from textfield and convert to double in Java? [duplicate]

Tags:

java

How to convert "۱۲۳۴۵" Persian string numbers to double?

I want get Persian number from a textfield and save it in a double variable. I tried:

product.setSellPrice(Double.parseDouble(txtProductSalePrice.getText()));

but this throws Caused by: java.lang.NumberFormatException: For input string:.

like image 748
vahid hasani Avatar asked Nov 09 '22 20:11

vahid hasani


1 Answers

You could breakdown the String into chars and iterate through the characters using, for example, toCharArray() and then convert each number to its English equivalent.

String number = "";
for (char c : txtProductSalePrice.toCharArray()) {
    if (c == "۱") {
        number.concat("1");
        continue;
    }
    if (c == "۲") {
        number.concat("2");
        continue;
    }
    ....
}
return new BigDecimal(number).doubleValue();

I'm sure this could probably be improved though, and I'm not entirely sure whether char will support non-Roman letters.

like image 55
mohammedkhan Avatar answered Nov 14 '22 22:11

mohammedkhan