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:
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With