java - 是否有任何 API 可以在 java 中格式化货币值?
问题描述
我有双倍的货币代码和价值。需要对货币进行格式化。我尝试使用 NumberFormat 和 Locale,但在这种情况下,例如 EURO 具有与国家/地区相关的不同区域设置。我怎样才能做到这一点?欧元有什么通用格式吗?
format.setCurrency(Currency.getInstance("EUR"));
format.setMaximumFractionDigits(2);
System.out.println(format.format(dbl));
Locale[] locales = NumberFormat.getAvailableLocales();
for(Locale lo : locales){
NumberFormat format = NumberFormat.getCurrencyInstance(lo);
if(NumberFormat.getCurrencyInstance(lo).getCurrency().getCurrencyCode().equals("EUR")){
System.out.println( NumberFormat.getCurrencyInstance(lo).getCurrency().getCurrencyCode()+"-"+lo.getDisplayCountry() +"-"+NumberFormat.getCurrencyInstance(lo).getCurrency().getSymbol() +format.format(dbl));
}
}```
Sorry previous question was closed.
解决方案
除非您真的需要手动执行此操作,否则我宁愿使用 java money
<dependency>
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>1.4.1</version>
<type>pom</type>
</dependency>
从未使用过它,但听起来它可以解决您的问题,有关更多信息,请查看文档https://github.com/JavaMoney/jsr354-ri/blob/master/moneta-core/src/main/asciidoc/userguide。文档
推荐阅读
- python - Pyinstaller:QWebEngineView 不能很好地与其他模块配合使用
- laravel - vue for循环中的saldo计数器给出无限循环错误
- c++ - 为什么我不能将此转换后的 directory_iterator 插入向量中?
- r - 如何获取字符串中出现频率最高的字符?
- amazon-web-services - 如何找到 AWS 流量镜像目标 ARN?
- python - 一个单词后的正则表达式,但不是换行
- excel - Excel VBA:带条件的动态查找
- javascript - 单击按钮时如何覆盖div onClick事件?
- r - dplyr 通过 R 中的常见行值合并数据帧
- kotlin - Kotlin 状态流未在具有视图模型范围的单元测试中发出