- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Locale(languageCode, countryCode) 构造函数将 BigDecimal 货币值转换为区域设置特定的货币格式,如下面的代码所示
public static String formatCurrency(BigDecimal amount, String languageCode, String countryCode) {
Format format = NumberFormat.getCurrencyInstance(new Locale(languageCode, countryCode));
String formattedAmount = format.format(amount);
logger.debug("Orginal Amount {} and Formatted Amount {}", amount, formattedAmount);
return formattedAmount;
}
现在根据优秀资源 Oracle Docs
The runtime environment has no requirement that all locales be supported equally by every locale-sensitive class. Every locale-sensitive class implements its own support for a set of locales, and that set can be different from class to class. For example, a number-format class can support a different set of locales than can a date-format class.
由于我的 languageCode 和 countryCode 是由用户输入的,当用户输入错误输入(例如 languageCode = de 和 countryCode = US)时,我该如何处理这种情况(或者说 NumberFormat.getCurrencyInstance 方法如何处理)。
它是否默认为某些 Locale ?如何处理这种情况。
谢谢。
最佳答案
根据@artie 的建议,我正在使用 LocaleUtil.isAvailableLocale 来检查语言环境是否存在。如果它是一个无效的区域设置,我将它发送给 en_US。这在一定程度上解决了问题。
但是,它仍然没有解决检查 NumberFormat 是否支持该 Locale 的问题。将接受解决此问题的任何其他答案。
public static String formatCurrency(BigDecimal amount, String languageCode, String countryCode) {
Locale locale = new Locale(languageCode, countryCode);
if (!LocaleUtils.isAvailableLocale(locale)) {
locale = new Locale("en", "US");
}
Format format = NumberFormat.getCurrencyInstance(locale);
String formattedAmount = format.format(amount);
logger.debug("Orginal Amount {} and Formatted Amount {}", amount, formattedAmount);
return formattedAmount;
}
关于java - 使用 Locale(languageCode, countryCode) 将 BigDecimal 格式化为 Locale 特定的货币字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46997969/
单击单选按钮后,我构建了一个用于更改语言的应用程序,但是当更改语言的页面打开时,它显示红色屏幕错误,并显示以下标题。 该错误是:-NoSuchMethodError:在null上调用了getter'l
我正在尝试在我的应用程序中实现本地化。 我的应用程序仅支持英语和印地语两种语言。但用户可以选择从设备设置中选择英语或印地语以外的任何语言。如果用户选择了任何其他语言,我想将应用程序区域设置设置为印地语
我正在寻找相当于这个 SO 问题/答案的 Dart : Get language name in that language from language code 换句话说,我宁愿避免手动检查 if
本文整理了Java中com.koolearn.klibrary.core.resources.ZLResource.languageCodes()方法的一些代码示例,展示了ZLResource.lan
我正在使用 Locale(languageCode, countryCode) 构造函数将 BigDecimal 货币值转换为区域设置特定的货币格式,如下面的代码所示 public static St
我是一名优秀的程序员,十分优秀!