简体   繁体   English

在Java中使用哪种舍入模式进行货币操作?

[英]Which rounding mode to be used for currency manipulation in java?

I have read on java site to use BigDecimal for currencies. 我在java网站上读过使用BigDecimal作为货币。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 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 这是最合适的,也是我们最广泛的

There is no "correct" mode, it depends on the business case. 没有“正确”模式,这取决于商业案例。 Examples: 例子:

  • When calculating annual taxes, the fractions are often cut off ( RoundingMode.FLOOR ). 在计算年度税时,分数通常会被切断( RoundingMode.FLOOR )。
  • When calculating a bonus, you might want to always round in favor of the customer ( RoundingMode.CEILING ). 计算奖金时,您可能希望始终支持客户( RoundingMode.CEILING )。
  • For taxes on a bill, you usually round HALF_UP 对于账单上的税,您通常HALF_UPHALF_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. RoundingMode文档包含很多不同模式如何工作的例子。

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. 也就是说, BigDecimal是在Java中使用的正确类型,因为它可以保留任何精度,而且它允许您选择最适合您的情况的舍入模式。

Most of the time BigDecimal is the only valid choice for currencies. 大多数时候, BigDecimal是货币的唯一有效选择。 But the choice of rounding strategy is not that obvious. 但是舍入策略的选择并不那么明显。

The default is HALF_EVEN which happens to be a good choice. 默认为HALF_EVEN ,这恰好是一个不错的选择。 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. 另一种常见的策略是HALF_UP ,它更直观但统计特性略差。

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. 还要注意,在很多时候(特别是在银行和保险业),舍入策略将由业务需求决定,对于各种用例通常是不同的。

Typically you'd use "half up" rounding, like so: 通常你会使用“half up”舍入,如下所示:

myBigDecimal.setScale(2, RoundingMode.HALF_UP);

This way, you'll round to two decimal places (which most currencies use, eg 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. 有关更多详细信息,请参阅javadoc

For the financial applications ROUND_HALF_EVEN is the most common rounding mode. 对于财务应用程序,ROUND_HALF_EVEN是最常见的舍入模式。 That mode avoids bias. 该模式避免了偏见。 But for display you should use NumberFormat class. 但是对于显示,你应该使用NumberFormat类。 This class will take care of localization issues for amounts in different currencies. 本课程将处理不同货币金额的本地化问题。 But NumberFormat accepts primitives only. 但是NumberFormat只接受原语。 So use last one if you can accept small accuracy change in transformations to a double. 因此,如果您可以接受转换为double的小精度更改,请使用最后一个。

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. 当显示金额然后除以适当的因子以得到非整数部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM