[英]What's the recommended way to format currency symbols in a locale-agnostic way?
I want to have a standard way of formatting prices regardless of the locale.无论语言环境如何,我都希望有一种标准的价格格式化方式。 Currently, the output format is locale-aware (as with
NumberFormat
):目前,输出格式是区域感知的(与
NumberFormat
一样):
Monetary amount![]() |
Format with locale es-ES![]() |
Format with locale en-US![]() |
---|---|---|
Money.of(1234, "EUR") ![]() |
1.234,00 € ![]() |
EUR1,234.00 ![]() |
Generated with this gist用这个要点生成
I've been able to get the desired output by overriding the Locale in the AmountFormatQueryBuilder
to force it use the locale I prefer :我已经能够通过覆盖
AmountFormatQueryBuilder
中的语言环境来强制它使用我喜欢的语言环境来获得所需的输出:
static String format(Money money, Locale locale) {
MonetaryAmountFormat formatter = MonetaryFormats.getAmountFormat(
AmountFormatQueryBuilder.of(locale)
.set(CurrencyStyle.SYMBOL)
.setLocale(EUR_OVERRIDDEN_LOCALE)
.build()
);
return formatter.format(money);
}
but this would require having a Map<Currency, Locale>
that will hold the desired locale for each currency that should be extracted from the MonetaryAmount
.但这需要有一个
Map<Currency, Locale>
来保存应该从MonetaryAmount
中提取的每种货币的所需语言环境。
This solution looks like a workaround and I just want to get some insights from the community.这个解决方案看起来像是一种解决方法,我只是想从社区中获得一些见解。
Ok, I think the answer to this question is here: https://github.com/JavaMoney/jsr354-ri/blob/master/moneta-core/src/main/asciidoc/userguide.adoc#42-registering-your-own-formats好的,我认为这个问题的答案在这里: https ://github.com/JavaMoney/jsr354-ri/blob/master/moneta-core/src/main/asciidoc/userguide.adoc#42-registering-your- 自有格式
By registering a new format using the JSR's SPI mechanism, we can override the default format and have it work the way we want通过使用 JSR 的 SPI 机制注册新格式,我们可以覆盖默认格式并让它按照我们想要的方式工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.