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

时间:2022-05-12 20:58:36

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

我在java网站上读过使用BigDecimal作为货币。 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

但是我们应该使用哪种舍入模式?这是最合适的,也是我们最广泛的

5 个解决方案

#1


45  

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

#2


17  

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

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

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

默认为HALF_EVEN,这恰好是一个不错的选择。这种算法被称为银行家的舍入(见这里的讨论)。

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.

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

#3


9  

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.

通过这种方式,你将四舍五入到两位小数(大多数货币都使用,例如美元和美分,显然有例外情况),你可以将价值调整为半美分或更多,而不到半美分将圆了下来。有关更多详细信息,请参阅javadoc。

#4


4  

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.

对于财务应用程序,ROUND_HALF_EVEN是最常见的舍入模式。该模式避免了偏见。但是为了显示你应该使用NumberFormat类。本课程将处理不同货币金额的本地化问题。但是NumberFormat只接受原语。因此,如果您可以接受转换为double的小精度更改,请使用最后一个。

#5


-8  

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.

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

#1


45  

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

#2


17  

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

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

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

默认为HALF_EVEN,这恰好是一个不错的选择。这种算法被称为银行家的舍入(见这里的讨论)。

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.

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

#3


9  

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.

通过这种方式,你将四舍五入到两位小数(大多数货币都使用,例如美元和美分,显然有例外情况),你可以将价值调整为半美分或更多,而不到半美分将圆了下来。有关更多详细信息,请参阅javadoc。

#4


4  

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.

对于财务应用程序,ROUND_HALF_EVEN是最常见的舍入模式。该模式避免了偏见。但是为了显示你应该使用NumberFormat类。本课程将处理不同货币金额的本地化问题。但是NumberFormat只接受原语。因此,如果您可以接受转换为double的小精度更改,请使用最后一个。

#5


-8  

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.

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