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