5

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:.

  • So you want to convert a persian text to an decimal output. Search some library or do it youself, I dont believe java has a default library to do that. – kunpapa Feb 03 '16 at 11:19
  • This answer is useful. http://stackoverflow.com/a/24407312/2413303 – EpicPandaForce Feb 03 '16 at 11:32
  • just mentioning this can help too http://stackoverflow.com/a/12568809/2930834 and deleting my answer. – Nakul Feb 03 '16 at 11:35

2 Answers2

1

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.

mohammedkhan
  • 953
  • 6
  • 14
0

If you use Calligraphy it's solve your problem. apply your precision-things to your text and this library do rest of things for you.

PS : you should use proper Farsi font

Amir
  • 16,067
  • 10
  • 80
  • 119