javaroundingbigdecimalrounding-errorbankers-rounding

Java rounding issue with BigDecimal and calculations


I have a data set which consists of BigDecimal numbers. I want to present this in a report. I have managed to generate a report with the following format. This is somewhat similar to a trial balance in accounting. But it is not! The data in the table have 4 decimal points in their values. But in the report it should be rounded to two decimal places. The report has to be accurate as well, hence the usage of BigDecimal.

+--------+---------+--------+
|Account | Debits  | Credits|
+--------+---------+--------+
|A       | 0.2760  | 0.1630 |
+--------+---------+--------+
|B       | 0.2770  | 0.2640 |
+--------+---------+--------+
|Total   | 0.5530  | 0.4270 |
+--------+---------+--------+

The real issue is when it comes to displaying the totals. The totals won't add up properly, after rounded.

+--------+---------+--------+
|Account | Debits  | Credits|
+--------+---------+--------+
|A       | 0.28    | 0.16   |
+--------+---------+--------+
|B       | 0.28    | 0.26   |
+--------+---------+--------+
|Total   | 0.55    | 0.43   |
+--------+---------+--------+

I am using the HALF_EVEN rounding mode because it is used for bookkeeping and also because this case involves financials as well.

I can add it up after rounding the values. then the report calculations will be OK. But it will not represent the actual value which is in the database (.553 is close to .55, but if I add it up after rounding the result would be .56, .553 is not close to .56).

Is there anyway to overcome this issue? I looked around to see how trial balance reports are made but I could not find any proper solution for this?

EDIT 1

I have seen trial balance reports in financial systems like this, do they make the calculations after rounding? I keep comparing this to Trial Balance report since this report is similar to one.

EDIT 2
Is it OK to make the calculation on rounded values? I have learnt that making the calculations on the rounded value is not recommended since the original values get discarded.

EDIT 3 Based on @MarcioB, @user3679868 and @JoopEggen decided to make the calculations based on the rounded values. The difference .047 (.56 - .553) is written off apparently (Audit standard)!


Solution

  • Because you are rounding each account if you want a total that is equal to the sum of the accounts you will have to compute the total using the rounded values for the accounts.