Reputation: 3077
I am getting the following value from my database when I do
resultSet.getdouble()
2.0006362409E9
I want to perform arithmetic operations on this number.
I tried to get the actual value using BigDecimal.toPlainString()
, then I am getting the following value
2000636240.900000095367431640625
When I try to convert BigDecimal.toPlainString() to double I am getting values again in scientific notation.
I want to divide this number -how can I solve this problem?
Upvotes: 0
Views: 397
Reputation: 140525
You don't necessarily have to convert to double in order to perform computations.
The wohle point of using BigDecimal is that it allows to deal with arbitrary large numbers regarding all aspects that numbers "need". Meaning: BigDecimal has various methods for doing computations, such as divide().
In other words: unless you are 100% sure that the numbers coming from that database fit into the range for Java double values - you might simply "stick" with using BigDecimal objects. Any computation that is possible with primitive doubles can also be done using the methods provided by the BigDecimal class.
And in case your numbers are in the double range (your example indicates so) - simply use Double.valueOf(String) in order to turn that string into a Double/double. Then you would not need that BigDecimal "detour" in the first place!
Upvotes: 4
Reputation: 15622
You have a misconception about internal number representation and user display.
To do calculations with large numbers like yours the BigDecimal
class it the right choice. Yes, you cannot use primitive operators (+
/-
...) and have to use the methods provided by the BigDecimal
class (add()
,subtract()
...). But in return you get almost infinite accuracy while calculating with primitive double
values has inherent failure because of fixed number length, especially when dealing with fractions.
Upvotes: 4