使用Moneta(JavaMoney)JSR354实现自定义MonetaryAmountFormat

编程入门 行业动态 更新时间:2024-10-27 01:31:01
本文介绍了使用Moneta(JavaMoney)JSR354实现自定义MonetaryAmountFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我真的很困惑如何使用Moneta JSR-354实现自定义 MonetaryAmountFormat 。

I'm really confused about how to customize a MonetaryAmountFormat using the Moneta JSR-354 implementation.

我的意图是能够解析 1.23 和 $ 3.45 为 MonetaryAmount s。

My intention is to be able to parse both 1.23 and $3.45 as MonetaryAmounts.

这是我的单元测试:

@Test public void testString() { Bid bid = new Bid("1.23"); assertEquals(1.23, bid.getValue(), 0.0); System.out.println(bid); bid = new Bid("$3.45"); assertEquals(3.45, bid.getValue(), 0.0); System.out.println(bid); }

这是我的班级:

public final class Bid { private static final CurrencyUnit USD = Monetary.getCurrency("USD"); private MonetaryAmount bid; /** * Constructor. * * @param bidStr the bid */ public Bid(String bidStr) { MonetaryAmountFormat format = MonetaryFormats.getAmountFormat( AmountFormatQueryBuilder.of(Locale.US) .set("pattern", "###.##") .build()); if (StringUtils.isNotBlank(bidStr)) { bidStr = bidStr.trim(); bidStr = bidStr.startsWith("$") ? bidStr.substring(1) : bidStr; try { bid = FastMoney.parse(bidStr, format); } catch (NumberFormatException e) { bid = FastMoney.of(0, USD); } } } /** * Constructor. * * @param bidDouble the bid */ public Bid(double bidDouble) { bid = FastMoney.of(bidDouble, USD); } public double getValue() { return bid.getNumber().doubleValue(); } }

我真的很想能够解析使用单个 MonetaryAmountFormat ,使用或不使用 $ bidStr ,但是在花了很多时间试图找出如何使 $ 可选之后,我放弃了。不幸的是,我甚至无法弄清楚如何将 1.23 解析为美元。 Moneta抛出 NullPointerException 。我应该使用 AmountFormatQueryBuilder 设置货币吗?使用 AmountFormatQueryBuilder 可以设置的所有键是什么?我搜索了文档,但找不到任何地方,除了几个常用键,如 pattern 。

I would have really liked to be able to parse the bidStr with or without the $ using the single MonetaryAmountFormat, but after spending a lot of time trying to find out how to make $ optional, I gave up. Unfortunately, I can't even figure out how to make 1.23 get parsed as USD. Moneta throws a NullPointerException. Am I supposed to set a currency using the AmountFormatQueryBuilder? What are all the keys that can be set using the AmountFormatQueryBuilder? I searched for documentation, but couldn't find any anywhere, except for a couple of common keys, like pattern.

推荐答案

尝试解析不合格的数字(如 1.23 )时,JavaMoney似乎不能正常工作。

JavaMoney does not seem to work well when trying to parse unqualified numbers (like 1.23).

默认情况下, MonetaryAmountFormat 希望您提供货币名称(例如 USD 1.23 ):

By default, the MonetaryAmountFormat wants you to provide the currency name (like USD 1.23):

MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.US); MonetaryAmount amount = format.parse("USD 1.23");

如果你设置 CurrencyStyle.SYMBOL ,那么您可以按货币名称或符号进行解析(因此, USD 1.23 或 $ 3.45 ):

If you set CurrencyStyle.SYMBOL, then you can parse by currency name or symbol (so, either USD 1.23 or $3.45):

AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(Locale.US) .set(CurrencyStyle.SYMBOL) .build(); MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); MonetaryAmount amount1 = format.parse("USD 1.23"); MonetaryAmount amount2 = format.parse("$3.45");

这可能是你用JavaMoney最接近的。

That is probably the closest you are going to get with JavaMoney.

如果你知道你只是从同一种货币获得数字,你可能最好在其他地方解析字符串(使用正则表达式或其他东西)并转换为一致的输入格式,因为(正如你发现的那样),你当JavaMoney不满意时,很容易得到 NullPointerException s没有解释。

If you know you are only getting numbers from the same currency, you might be better off parsing the string elsewhere (with regexs or something) and converting to a consistent input format because (as you discovered), you easily get NullPointerExceptions with no explanation when JavaMoney is unhappy.

就可用密钥而言,你可以偷看at org.javamoney.moneta.format.AmountFormatParams 上的常量:模式, groupingSizes , groupingSeparators 。

As far as the available keys, you can peek at the constants on org.javamoney.moneta.format.AmountFormatParams: pattern, groupingSizes, groupingSeparators.

设置格式时,务必注意必须使用通用货币符号:¤。例如,你可以做 .set(pattern,¤#,## 0.00)。

When setting the format, it is important to note that you must use the generic currency sign: ¤. For instance, you might do .set("pattern", "¤#,##0.00").

最后,不是直接使用Moneta RI中的 FastMoney 类,而是可以从 MonetaryAmountFormat 中解析:

Finally, rather than directly using the FastMoney class from the Moneta RI, you can parse right from a MonetaryAmountFormat:

// rather than using Monata directly: MonetaryAmount amount = FastMoney.parse(bidStr, format); // use the API: MonetaryAmount amount = format.parse(bidStr);

更多推荐

使用Moneta(JavaMoney)JSR354实现自定义MonetaryAmountFormat

本文发布于:2023-10-07 17:08:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   JavaMoney   Moneta   MonetaryAmountFormat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!