我正在尝试创建一个使用货币单位符号的MonetaryAmountFormat:
MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(
AmountFormatQueryBuilder.of(Locale.GERMANY)
.set(org.javamoney.moneta.format.CurrencyStyle.SYMBOL)
.set("pattern", "#,##0.##¤")
.build()
);(摘自How to format MonetaryAmount with currency symbol?和Customizing a MonetaryAmountFormat using the Moneta (JavaMoney) JSR354 implemenation)。
java/maven项目在运行时(而不是编译时)范围内依赖于moneta。似乎类CurrencyStyle和它的值SYMBOL是moneta的一部分,而不是java-money API的一部分。因此,代码无法编译。
我创建了这个丑陋的变通方法:
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()
);有没有可能在没有这种攻击的情况下创建一个使用货币单位符号的MonetaryAmountFormat?
发布于 2018-06-24 14:37:42
也许使用DecimalFormat作为MonetaryAmountFormat的替代品是一种选择。
缺点:
Number和MonetaryAmount之间的
MonetaryAmount对象)示例:
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()https://stackoverflow.com/questions/50989627
复制相似问题