You need to use the doubleValue() method to get the double value from a BigDecimal object.
BigDecimal bd; // the value you get/receive from your source
// In case there is a chance that the incoming BigDecimal value is null,
// please add an appropriate null check before you invoke the doubleValue() method on that object.
double d = bd.doubleValue(); // The "double" you want
Answer from Rahul on Stack OverflowYou need to use the doubleValue() method to get the double value from a BigDecimal object.
BigDecimal bd; // the value you get/receive from your source
// In case there is a chance that the incoming BigDecimal value is null,
// please add an appropriate null check before you invoke the doubleValue() method on that object.
double d = bd.doubleValue(); // The "double" you want
You can convert BigDecimal to double using .doubleValue(). But believe me, don't use it if you have currency manipulations. It should always be performed on BigDecimal objects directly. Precision loss in these calculations are big time problems in currency related calculations.
java - Converting BigDecimal to double value - Stack Overflow
java - Convert double to BigDecimal and set BigDecimal Precision - Stack Overflow
calculating the a bigdecimal to the power of double
Double vs BigDecimal in financial programming
Use long or big decimal. Performance problems will most probably arise elsewhere before you factor a big decimal as the culprit
More on reddit.comThe reason of such behaviour is that the string that is printed is the exact value - probably not what you expected, but that's the real value stored in memory - it's just a limitation of floating point representation.
According to javadoc, BigDecimal(double val) constructor behaviour can be unexpected if you don't take into consideration this limitation:
The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
So in your case, instead of using
double val = 77.48;
new BigDecimal(val);
use
BigDecimal.valueOf(val);
Value that is returned by BigDecimal.valueOf is equal to that resulting from invocation of Double.toString(double).
It prints 47.48000 if you use another MathContext:
BigDecimal b = new BigDecimal(d, MathContext.DECIMAL64);
Just pick the context you need.
Everyone says that BigDecimal should be used when dealing with money but it’s much slower and takes more memory than double. I would think this would be especially important in high frequency low-latency applications like trading. Do people actually use BigDecimal in such systems or do they use doubles with some kind of workaround to handle the precision issue?
Edit: I do have experience working on trading and risk systems and I see doubles used much more often than BigDecimal so I was curious to see if this is more common in actual practice. Most of the systems I worked on only need precision to the penny so I wonder if that’s the reason?
Also, BigDecimal is a pain to use and code written with it look much uglier than plain doubles.