android - BigDecimal 在带有本地化的 Android 4.0.3 中给出 NumberFormatException

标签 android localization bigdecimal decimalformat

我正在使用波斯语(locale-“fa”)和普什图语(locale-“ps”)语言在我的应用程序中进行本地化。当我尝试使用 Decimal Foramt 转换 BigDecimal 中的 double 值时,它会在 4.0 中给出 NumberFormatException .3 波斯语。

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

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 ۱۶۶۳۰۸٫۳۷

实际金额为 166308.37,当我将语言更改为 persian("fa") 并对其进行格式化时,它给出的金额类似于 4.0.3 中的 166308٫37。 所以这个字符串不能转换为 BigDecimal 并给出 NumberFormatException

奇怪的是,除了 4.0.3,它工作得很好。

最佳答案

试试这段代码..

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();
}

关于android - BigDecimal 在带有本地化的 Android 4.0.3 中给出 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657852/

相关文章:

java - Mockito 和 Assertions - 两个相同的 BigDecimals...不相等

mysql - 无法将 bigdecimal 分配给模型中的字段

android - 如何在 fragment 和非父 Activity 之间传输数据?

android - 无法从 SQLiteCursorLoader 转换为 Loader<Cursor>

具有 unicode 代码范围规范的 Java 类

ios - Googlecast 标签未本地化为设备语言

iphone - xcode iOS 获取所有本地化的列表

android - 侦察兵 SDK : Multi language support for Advisor

android - 从 asynctask postexecute 访问 fragment 方法

java - 为什么 toString 无法在不可变的 BigDecimal 上产生正确的值?