简体   繁体   English

JavaMoney:设置CurrencyStyle时不依赖moneta的编译时

[英]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: 缺点:

  • Conversion between Number and MonetaryAmount must be done manually NumberMonetaryAmount之间的转换必须手动完成
  • Works only when you don't have changing currency units (unit is taken from the format, not from the 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.

相关问题 Javamoney.moneta.Money 序列化/反序列化为 null 尽管我有 jackson-datatype-money 依赖项 - Javamoney.moneta.Money serialized/deserialized as null although I have jackson-datatype-money dependency 编译时和运行时之间的依赖注入 - Dependency injection between compile-time and run-time 编译Java类时禁用编译时依赖性检查 - Disabling compile-time dependency checking when compiling Java classes 是否可以使用spring Java @Configuration避免编译时依赖性? - Is it possible to avoid compile-time dependency using spring Java @Configuration? 是否有编译时依赖注入工具支持 kotlin(或 java)的 generics? - Is there a compile-time dependency injection tool that supports generics for kotlin (or java)? 如何在MonetaryAmount(org.javamoney.moneta)中四舍五入数字 - How to round number in MonetaryAmount (org.javamoney.moneta) 使用Moneta(JavaMoney)JSR354实现自定义MonetaryAmountFormat - Customizing a MonetaryAmountFormat using the Moneta (JavaMoney) JSR354 implemenation 在没有拆分包的情况下实现(编译时)插件架构 - Implementing a (compile-time) plugin architecture without split packages Java和编译时常量 - Java & Compile-Time Constants 编译时注释处理 - Compile-time annotation processing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM