gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:37:23 26 4
gpt4 key购买 nike

我正在使用波斯语(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com