我在数据库中存储了一个与货币金额相关的值,例如 10.0。我还可以访问货币/货币代码。当我不知道语言环境时,如何使用 NumberFormat/DecimalFormat/(other?) 进行格式化?根据文档,它将选择一个不适用于外币的默认语言环境。
最佳答案
JasonTrue 是正确的,但您可以覆盖 NumberFormat 语言环境的货币:
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
//the user may have selected a different currency than the default for their locale
Currency currency = Currency.getInstance("GBP");
numberFormat.setCurrency(currency);
numberFormat.format(amount);
关于Java:货币到语言环境的映射可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/758255/