用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
一路走来。我听说有些人创建了他们自己的现金
或 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.
编辑:正如Don在他的回答中提到,有像 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屋!