Java - 金钱类的名称
我应该如何命名我的类,将 CAD/USD 的成本/价格/金额存储为美元和美分的单独 long
?我有“PriceInDollars”,但我不太喜欢它,而且我总是忘记它。有什么想法吗?
编辑:显然我应该使用 BigDecimal,而不是重新发明轮子。我想我就用那个吧。不过,我在尝试制作自己的课程时确实学到了很多东西。
请在此处查看我的其他问题!
What should I name my class that stores costs/prices/amounts of money in CAD/USD as separate long
s for dollars and cents? I has "PriceInDollars", but I don't really like that, and I always forget it. Any ideas?
EDIT: Apparently I should use BigDecimal
, not reinvent the wheel. I guess I'll just use that. I did learn a lot trying to make my own class though.
Please see my other question here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我会选择
价格
或成本
。无论您选择哪一个,都可以对其进行子类化以获得更具体的信息。Money
和Currency
看起来更像是价格的属性,并不适合作为价格本身的名称。只是我的 2 美分(无法抗拒双关语)。I would go with either
Price
orCost
. Whichever one you choose, you can subclass it to get more specific.Money
andCurrency
seem more like they would be attributes of the price, not really suitable as the name of the price itself. Just my 2 cents (couldn't resist the pun).首先,我不同意所有说使用 BigDecimal 的人。即使您的“后备存储”是 BigDecimal(这是非常明智的),您也应该使用自己的类,因为具有 BigDecimal 字段、方法参数等不传达任何语义。常见重用类型(例如货币金额)应该有自己的类。
综上所述,不要自己写。有很多相关的库已经存在,并且已经为您考虑了许多问题。就我个人而言,我会从 Stephen Colebourne(因 Joda-Time 出名)编写的 Joda-Money 开始。
Firstly, I disagree with everyone who says to use
BigDecimal
. Even if your 'backing storage' isBigDecimal
(which is very sensible), you should use your own class because havingBigDecimal
fields, method parameters, etc, conveys no semantics. Commonly re-used types like currency amounts should have their own classes.All that said, don't write your own. There are plenty of libraries for this which already exist and have thought through many of the issues for you. Personally, I'd start with Joda-Money, by Stephen Colebourne (of Joda-Time fame).
货币价值:-)
MonetaryValue :-)
货币
金钱
价格
价值
(不过我不会)Currency
Money
Price
Value
(I wouldn't, though)为什么
金钱
是一个糟糕的选择?在我看来,将整数部分和小数部分与 java.util.Currency 一起封装到一个类中是比 BigDecimal 更好的设计。我不喜欢任何以“InDollars”结尾的东西,因为它不必要地损害了您对美元或加元的设计。如果这个想法更普遍,为什么要这样做呢?
Why is
Money
a bad choice? Encapsulating whole and fractional parts along withjava.util.Currency
into a class is a better design thanBigDecimal
, IMO.I don't like anything that ends in "InDollars", because it unnecessarily prejudices your design to USD or CAD. Why do that if the idea is more general?
我会使用“现金”
http://www.answers.com/topic/cash
纸币或硬币形式的货币;货币。
以货币或支票支付商品或服务。
它不特定于任何货币类型,您可以添加许多动词以进行转换等。
I would use "Cash"
http://www.answers.com/topic/cash
Money in the form of bills or coins; currency.
Payment for goods or services in currency or by check.
it's not specific to any currency type, lots of verbs you can add to it for conversions and such.
我已将我的称为
MonetaryAmount
。I've called mine
MonetaryAmount
.我将其称为
Amount
,其中包含一个java.util.Currency
和一个带有单位的long
。这是我在不同项目中使用过的我自己的
Amount
类。它实现了 Comparable 并具有 toString 方法来格式化显示的金额(尽管它在格式化金额时不会考虑您的区域设置)。I would call it
Amount
, holding ajava.util.Currency
and along
with the units.Here is my own
Amount
class that I've used in different projects. It implementsComparable
and has atoString
method to format the amount for display (although it does not take your locale into account for formatting the amount).