English 中文(简体)
在java,哪些四舍五入模式用于操纵货币?
原标题:Which rounding mode to be used for currency manipulation in java?

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

但是,我们应当采用什么样的四舍五入的模式? 它是最合适的,而且最广泛地是我们

问题回答

没有“正确”模式,它取决于商业情况。 实例:

  • 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 载有不同模式如何运作的许多实例。

为了找到更好的答案,你必须告诉我们你想要实现什么。

尽管如此,<代码>BigDecimal 是 Java使用的适当类型,因为它可以保持任何精确度,而且可以让你选择最适合你情况的环绕模式。

大部分时间<代码>BigDecimal是货币的唯一有效选择。 但选择四舍五入的战略并非显而易见。

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

另一项共同战略是:HALF_UP/a>,后者比较直观,但统计特征略有恶化。

还指出,在很多时候(特别是在银行和保险方面),该回合战略将受到商业要求的制约,对不同的使用情况往往有所不同。

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签