I am trying to establish some concise overview of what options for precise caluclations we have in JAVA+SQL. So far I have found following options:
any other options?
Problem statement: I need precise financial calculations that would involve using very big (billions) and very small numbers (0.0000004321), and also dividing values that are very similar to each other, so for sure I need precision of BigDecimal.
On the other side, I want to retain ease of use that doubles have in functions (i work on arrays from decimal SQL data), so calculations like: (a[i] - b[i])/b[i] etc. etc. that are further used in other calculations. and I'd like to have users to be able to desing their own formulas as they need them (using common math statements)
i am keen to use "formatting" solution for String.format, but this makes code not very readable ( using String.format() for each variable...).
Many thanks for suggestion of how to deal with the stuff.
There is no way to get BigDecimal
precision on a double
. double
s have double precision.
If you want to guarantee precise results use BigDecimal
.
You could create your own variant using a long
to store the integer part and an int
to store the fractional part - but why reinvent the wheel.
Any time use double
s you stand to stuffer from double precision issues. If you use them in a single place you might as well use them everywhere.
Even if you only use them to represent data from the database then will round the data to double precision and you will lose information.