Java - 金钱类的名称

发布于 2024-08-03 21:52:11 字数 287 浏览 2 评论 0原文

我应该如何命名我的类,将 CAD/USD 的成本/价格/金额存储为美元和美分的单独 long ?我有“PriceInDollars”,但我不太喜欢它,而且我总是忘记它。有什么想法吗?

编辑:显然我应该使用 BigDecimal,而不是重新发明轮子。我想我就用那个吧。不过,我在尝试制作自己的课程时确实学到了很多东西。

请在此处查看我的其他问题!

What should I name my class that stores costs/prices/amounts of money in CAD/USD as separate longs 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

蓝色星空 2024-08-10 21:52:11

我会选择价格成本。无论您选择哪一个,都可以对其进行子类化以获得更具体的信息。

MoneyCurrency 看起来更像是价格的属性,并不适合作为价格本身的名称。只是我的 2 美分(无法抗拒双关语)。

I would go with either Price or Cost. Whichever one you choose, you can subclass it to get more specific.

Money and Currency 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).

恋你朝朝暮暮 2024-08-10 21:52:11

首先,我不同意所有说使用 BigDecimal 的人。即使您的“后备存储”是 BigDecimal(这是非常明智的),您也应该使用自己的类,因为具有 BigDecimal 字段、方法参数等不传达任何语义。常见重用类型(例如货币金额)应该有自己的类。

综上所述,不要自己写。有很多相关的库已经存在,并且已经为您考虑了许多问题。就我个人而言,我会从 Stephen Colebourne(因 Joda-Time 出名)编写的 Joda-Money 开始。

Firstly, I disagree with everyone who says to use BigDecimal. Even if your 'backing storage' is BigDecimal (which is very sensible), you should use your own class because having BigDecimal 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).

只是在用心讲痛 2024-08-10 21:52:11

货币价值:-)

MonetaryValue :-)

暮年慕年 2024-08-10 21:52:11
  • 货币
  • 金钱
  • 价格
  • 价值(不过我不会)
  • Currency
  • Money
  • Price
  • Value (I wouldn't, though)
七婞 2024-08-10 21:52:11

为什么金钱是一个糟糕的选择?在我看来,将整数部分和小数部分与 java.util.Currency 一起封装到一个类中是比 BigDecimal 更好的设计。

我不喜欢任何以“InDollars”结尾的东西,因为它不必要地损害了您对美元或加元的设计。如果这个想法更普遍,为什么要这样做呢?

Why is Money a bad choice? Encapsulating whole and fractional parts along with java.util.Currency into a class is a better design than BigDecimal, 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?

野味少女 2024-08-10 21:52:11

我会使用“现金”

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.

南笙 2024-08-10 21:52:11

我已将我的称为 MonetaryAmount

I've called mine MonetaryAmount.

才能让你更想念 2024-08-10 21:52:11

我将其称为 Amount,其中包含一个 java.util.Currency 和一个带有单位的 long

这是我在不同项目中使用过的我自己的Amount。它实现了 Comparable 并具有 toString 方法来格式化显示的金额(尽管它在格式化金额时不会考虑您的区域设置)。

I would call it Amount, holding a java.util.Currency and a long with the units.

Here is my own Amount class that I've used in different projects. It implements Comparable and has a toString method to format the amount for display (although it does not take your locale into account for formatting the amount).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文