开发者

Storing monetary values as Doubles but using BigDecimal to calculate values

Currently s开发者_StackOverflowome of our Hibernate entities are using Doubles to store monetary amounts, the database stores this value as a numeric(10,2). The calculation of these monetary double amounts are always calculated using BigDecimals and then the entity value is set as a double.

Example:

Order order = new Order();
OrderLineItem line = new OrderLineItem(1.0, 450.00);
BigDecimal orderValue = BigDecimal.valueOf(line.getQty())
                        .multiply(BigDecimal.valueOf(line.getAmount()));
order.setOrderTotal(orderValue.setScale(2, ROUND_HALF_EVEN).doubleValue());

This has worked fine so far, however I'm wondering if this has any potential risks for some sort of rounding error.

Thoughts?


As long as every piece of your information pipeline uses BigDecimal.valueOf and BigDecimal(double) is used absolutely nowhere then the implicit rounding of valueOf might prevent the worst. But do you really know how JPA and/or JDBC translate between numeric(10,2) and double? And are you sure, that this will not change in the future?

Another point: valueOf and doubleValue work with intermediate String representations. This might be a performance and garbage collection issue in some situations.

Besides of this there are enough examples for problems with double - simply look to the "Related" section on the right side. Or look at this blog where even simple stuff can wreck havoc.

So the situation is a little bit like speeding: Some don't do it, some do it sometimes and hope nothing happens and some... I think you get it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜