Pregunta

I am doing localization in my app with Persian(locale-"fa") and Pashto(locale-"ps") languages.. When i am trying to convert double value in BigDecimal using Decimal Foramt then it gives NumberFormatException in 4.0.3 with Persian language.

Following is my code to get two decimal places:

public static String roundTwoDecimals(double d) {

    System.out.println("===================Amount in double "+d);
    DecimalFormat twoDForm = new DecimalFormat("#############.##");
    String format=twoDForm.format(d);
    System.out.println("===================Amount after formatting "+format);
    return new BigDecimal(twoDForm.format(d)).toPlainString();
}

LogCat:

03-26 15:19:29.714: I/System.out(1475): ===================Amount in double 166308.37
03-26 15:19:29.723: I/System.out(1475): ===================Amount after formatting ۱۶۶۳۰۸٫۳۷

Actual amount is 166308.37 and when i change language to persian("fa") and format it, it gives amount like ۱۶۶۳۰۸٫۳۷ in 4.0.3 . So this string can not be converted to BigDecimal and gives NumberFormatException.

And strange problem is that except 4.0.3, it works perfectly fine.

¿Fue útil?

Solución

Try this code..

public static String roundTwoDecimals(double d) {

    NumberFormat nf=NumberFormat.getCurrencyInstance(Locale.ENGLISH);
    DecimalFormat df = (DecimalFormat)nf;
    df.applyPattern("#############.##");
    String output = df.format(d);

    return new BigDecimal(output).toPlainString();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top