java中的货币操作使用哪种舍入模式?

发布于 2024-12-17 16:09:07 字数 269 浏览 3 评论 0原文

我在 java 网站上读到过使用 BigDecimal 来表示货币。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts /datatypes.html

但是我们应该使用什么舍入模式呢?这是最合适、应用最广泛的一种

I have read on java site to use BigDecimal for currencies.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

But what rounding mode we should use with? which is the most appropriate one and most widely us

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

高跟鞋的旋律 2024-12-24 16:09:07

没有“正确”的模式,这取决于业务案例。示例:

  • 计算年税时,分数通常会被截断 (RoundingMode.FLOOR)。
  • 计算奖金时,您可能希望始终对客户有利 (RoundingMode.CEILING)。
  • 对于帐单上的税费,您通常会舍入HALF_UP
  • 在进行复杂的财务模拟时,您根本不想舍入。

RoundingMode 文档包含很多示例,说明了不同的模式有效。

为了获得更好的答案,您必须告诉我们您想要实现什么。

也就是说,BigDecimal 是在 Java 中使用的正确类型,因为它可以保留任意数量的精度,而且它允许您选择最适合您情况的舍入模式。

There is no "correct" mode, it depends on the business case. Examples:

  • When calculating annual taxes, the fractions are often cut off (RoundingMode.FLOOR).
  • When calculating a bonus, you might want to always round in favor of the customer (RoundingMode.CEILING).
  • For taxes on a bill, you usually round HALF_UP
  • When doing complex financial simulations, you don't want to round at all.

The documentation of RoundingMode contains a lot of examples how the different modes work.

To get a better answer, you must tell us what you want to achieve.

That said, BigDecimal is the correct type to use in Java because it can preserve any amount of precision plus it lets you chose the rounding mode most suitable for your case.

淡墨 2024-12-24 16:09:07

大多数时候,BigDecimal 是货币的唯一有效选择。但舍入策略的选择并不那么明显。

默认值为 HALF_EVEN是一个不错的选择。该算法被称为“银行家舍入”(参见此处的讨论)。

另一种常见策略是 HALF_UP 这是更直观,但统计特性稍差。

另请注意,在很多时候(尤其是在银行和保险领域),舍入策略将由业务需求决定,通常针对不同的用例而有所不同。

Most of the time BigDecimal is the only valid choice for currencies. But the choice of rounding strategy is not that obvious.

The default is HALF_EVEN which happens to be a good choice. This algorithm is known as bankers' rounding (see discussion here).

Another common strategy is HALF_UP which is more intuitive but has slightly worse statistical characteristics.

Also note that in many times (especially in banking and insurances) the rounding strategy will be dictated by business requirements, often different for various use-cases.

北风几吹夏 2024-12-24 16:09:07

通常,您会使用“半向上”四舍五入,如下所示:

myBigDecimal.setScale(2, RoundingMode.HALF_UP);

这样,您将四舍五入到小数点后两位(大多数货币都使用这一点,例如美元和美分,显然有例外),并且您将四舍五入值,使得半美分或以上将向上舍入,而少于半美分将向下舍入。有关详细信息,请参阅javadoc细节。

Typically you'd use "half up" rounding, like so:

myBigDecimal.setScale(2, RoundingMode.HALF_UP);

This way, you'll round to two decimal places (which most currencies use, e.g. dollars and cents, obviously there are exceptions), and you'll round values such that half a cent or more will round up while less than half a cent will round down. See the javadoc for more details.

只是在用心讲痛 2024-12-24 16:09:07

对于金融应用程序,ROUND_HALF_EVEN 是最常见的舍入模式。这种模式避免了偏见。
但为了显示,您应该使用 NumberFormat 类。此类将处理不同货币金额的本地化问题。但 NumberFormat 仅接受基元。因此,如果您可以接受转换为双精度的小精度变化,请使用最后一个。

For the financial applications ROUND_HALF_EVEN is the most common rounding mode. That mode avoids bias.
But for display you should use NumberFormat class. This class will take care of localization issues for amounts in different currencies. But NumberFormat accepts primitives only. So use last one if you can accept small accuracy change in transformations to a double.

迎风吟唱 2024-12-24 16:09:07

您永远不应该对货币使用小数类型。使用整数类型。这可以保持准确性,避免与浮点数相关的舍入误差。

当显示金额时,然后除以适当的因子以获得非整数部分。

You should never use a decimal type for currencies. Use an integer type. This maintains accuracy buy avoiding rounding error associated with floats

When display an amount then divide by the appropriate factor to get the non-integral portion.

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