[英]JavaMoney: set CurrencyStyle without a compile-time dependency on moneta
I'm trying to create a MonetaryAmountFormat
that uses the currency unit symbol: 我正在尝试创建使用货币单位符号的
MonetaryAmountFormat
:
MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(
AmountFormatQueryBuilder.of(Locale.GERMANY)
.set(org.javamoney.moneta.format.CurrencyStyle.SYMBOL)
.set("pattern", "#,##0.##¤")
.build()
);
(Taken from How to format MonetaryAmount with currency symbol? and Customizing a MonetaryAmountFormat using the Moneta (JavaMoney) JSR354 implemenation ). (摘自如何使用货币符号格式化MonetaryAmount?以及使用Moneta(JavaMoney)JSR354实现自定义MonetaryAmountFormat )。
The java/maven project has a dependency on moneta in runtime (not compile-time) scope. java / maven项目在运行时(而非编译时)范围内对moneta有依赖性。 It seems that the class
CurrencyStyle
and its value SYMBOL
are part of moneta, the java-money reference implementation, and not part of the java-money API. 似乎
CurrencyStyle
类及其值SYMBOL
是moneta(java-money参考实现)的一部分,而不是java-money API的一部分。 Thus, the code does not compile. 因此,代码无法编译。
I created this ugly workaround: 我创建了这个丑陋的解决方法:
String currencyStyle = "org.javamoney.moneta.format.CurrencyStyle";
final Enum<?> SYMBOL = Enum.valueOf((Class<? extends Enum>) Class.forName(currencyStyle), "SYMBOL");
MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(
AmountFormatQueryBuilder.of(Locale.GERMANY)
.set(currencyStyle, SYMBOL)
.set("pattern", "#,##0.##¤")
.build()
);
Is it possible to create a MonetaryAmountFormat
that uses the currency unit symbol without this hack? 是否有可能创建一个使用货币单位符号的
MonetaryAmountFormat
,而不会发生这种情况?
Maybe using DecimalFormat
as alternative to MonetaryAmountFormat
is an option. 也许可以使用
DecimalFormat
替代MonetaryAmountFormat
。
Drawbacks: 缺点:
Number
and MonetaryAmount
must be done manually Number
和MonetaryAmount
之间的转换必须手动完成 MonetaryAmount
object) MonetaryAmount
对象获取的) Example: 例:
NumberFormat format = new DecimalFormat("#,##0.##¤", DecimalFormatSymbols.getInstance(Locale.GERMANY));
// format
MonetaryAmount source = ...;
String formattedAmount = format.format(source.getNumber());
// parse
Number numberAmount = format.parse(formattedAmount);
MonetaryAmount target = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(numberAmount).create()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.