是否有任何API格式化货币值在java?



我有双精度的货币代码和值。需要为货币做格式化。我尝试了NumberFormat和Locale,但在这种情况下,例如EURO有不同的与国家相关的Locale。我怎样才能做到这一点呢?欧元有什么通用格式吗?

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货币

<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.adoc

不需要导入您需要学习和增加应用程序大小的外部java库。可以使用带有两个参数的构造函数来使用DecimalFormat,第一个是模式,第二个是要使用的符号:

使用给定的模式和符号创建一个DecimalFormat。当您需要完全自定义格式的行为时,请使用此构造函数。

你可以在这里找到官方指南的详细信息

下面是一个工作示例:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
symbols.setCurrency(Currency.getInstance("EUR"));
DecimalFormat df = new DecimalFormat("¤###,###.00", symbols);
System.out.println(df.format(5000.4));  // Will print €5.000,40    

下面是对模式¤###,###的描述。

¤       is the currency symbol (in our case will be replaced by the EUR symbol)
###,### is the integer part of the number. Digits are grouped in group of 3 
.00     is the decimal part of the number. If less than 2 decimal numbers are presented zeroes are 
added to the string so to have exactly 2 decimal numbers 

这个例子不关心区域设置,因为格式化数字中使用的所有符号都被显式替换。

您似乎在要求一个国际标准来显示以欧元表示的金额。

对于官方,正式,法律文本,ISO标准EUR(如美元兑换美元)是必需的。用于商业和其他人类领域-

但是每种语言似乎都保持了其旧货币的格式:英国的欧元在金额之前,许多其他欧洲语言在金额之后。小数分隔符和千位分隔符也是如此,它们因国家而异。

欧元符号和金额之间有一个不间断的空格。因此,使用广泛使用的十进制分隔符(逗号!)的SI标准,可以得到:

9.999,99u00A0€

试试Locale.FRANCEGERMANY是否足够。

注意:这(将货币放置在右侧)允许将带有货币的金额放置在单个右对齐的列中。

在美国,为了避免因为上述物品只值10美元而被起诉,人们可能会使用不间断的空格,或者不间断的半空格:

9.999.999,99u00A0€
9u202F999u202F999,99u00A0€

看起来或多或少像(而不是行尾换行):

9.999.999,99 €
9 999 999,99 €

瑞士(典型)的解决方案是使用撇号'作为千位分隔符。

逗号是IMHO - us -在法律上是没有问题的,因为SI,国际单位制(d' unitemacs),是一个有代表性的标准。

欧分没有分符号,但以欧元表示。

  • U+20AC =€(欧元符号)
  • U+A0 =不间断空白
  • U+202F =不间断半空格

所以,我不知道泛欧标准。但我不是金融专家。

最新更新