BigDecimal在Android 4.0.3中提供了具有本地化的NumberFormatException

Kin*_*hah 5 android localization bigdecimal decimalformat

我在我的应用程序中使用波斯语(locale-"fa")和Pashto(locale-"ps")语言进行本地化.当我尝试使用Decimal Foramt在BigDecimal中转换double值时,它在4.0.3中给出NumberFormatException波斯语.

以下是获取两位小数的代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

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 ?????????
Run Code Online (Sandbox Code Playgroud)

实际金额是166308.37,当我将语言改为波斯语("fa")并格式化时,它给出?????????了4.0.3中的金额.所以这个字符串无法转换为BigDecimal并给出NumberFormatException.

奇怪的是,除了4.0.3之外,它的工作原理非常好.

Nir*_*ari 5

试试这个代码..

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 好,你找到了解决你的自己,Nirav :) (3认同)