I have read on java site to use BigDecimal
for currencies.
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
但是,我们应当采用什么样的四舍五入的模式? 它是最合适的,而且最广泛地是我们
没有“正确”模式,它取决于商业情况。 实例:
RoundingMode.FLOOR
).RoundingMode.CEILING
).HALF_UP
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>,后者比较直观,但统计特征略有恶化。
还指出,在很多时候(特别是在银行和保险方面),该回合战略将受到商业要求的制约,对不同的使用情况往往有所不同。
通常,你使用“哈龙”四舍五入,如:
myBigDecimal.setScale(2, RoundingMode.HALF_UP);
这样,你将四舍五入到两个小数点(多数货币使用,例如美元和百分数,显然有例外),而且你将四舍五入的数值,即一半或一半以上将四舍五入。 See . javadoc for more details.
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.
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...