用 Java 表示货币价值 [英] Representing Monetary Values in Java
问题描述
我了解 BigDecimal 是在 Java 中表示货币价值的推荐最佳实践.你用什么?有没有您更喜欢使用的更好的库?
I understand that BigDecimal is recommended best practice for representing monetary values in Java. What do you use? Is there a better library that you prefer to use instead?
推荐答案
BigDecimal
一路.我听说有些人创建了他们自己的 Cash
或 Money
类,这些类用货币封装了现金价值,但在皮肤下它仍然是一个 BigDecimal
代码>,可能带有 BigDecimal.ROUND_HALF_EVEN
四舍五入.
BigDecimal
all the way. I've heard of some folks creating their own Cash
or Money
classes which encapsulate a cash value with the currency, but under the skin it's still a BigDecimal
, probably with BigDecimal.ROUND_HALF_EVEN
rounding.
正如唐在他的回答中提到的a>,有像 timeandmoney 这样的开源项目,虽然我赞赏他们试图阻止开发人员重新发明轮子,我只是对 pre-alpha 库没有足够的信心在生产环境中使用它.此外,如果你深入研究,你会看到 他们也使用 BigDecimal
.
As Don mentions in his answer, there are open sourced projects like timeandmoney, and whilst I applaud them for trying to prevent developers from having to reinvent the wheel, I just don't have enough confidence in a pre-alpha library to use it in a production environment. Besides, if you dig around under the hood, you'll see they use BigDecimal
too.
这篇关于用 Java 表示货币价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!