Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigDecimal gives NumberFormatException in Android 4.0.3 with localization

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.

like image 615
Kinjal Shah Avatar asked Mar 26 '14 10:03

Kinjal Shah


Video Answer


1 Answers

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();
}
like image 156
Nirav Bhandari Avatar answered Sep 29 '22 03:09

Nirav Bhandari