문제

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.

도움이 되었습니까?

해결책

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top