How we convert BigDecimal
into Double
in java? I have a requirement where we have to use Double as argument but we are getting BigDecimal
so i have to convert BigDecimal
into Double
.
3 Answers
You need to use the doubleValue()
method to get the double value from a BigDecimal object.
BigDecimal bd; // the value you get
double d = bd.doubleValue(); // The double you want
-
1Hi, i used same, But the problem is both anser are different my decimal output was 13555261857.79 and when converted to double its 1.355526185779E10 .. How do i get same output after converting decimal to double Aug 2, 2020 at 5:57
-
@user13926345 - Both are the same values. Just that the first output is the normal representation of the value and the other is the scientific representation.– RahulAug 4, 2020 at 8:46
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.
Use doubleValue method present in BigDecimal class :
double doubleValue()
Converts this BigDecimal
to a double
.
new BigDecimal(2).doubleValue()
doubleValue()
. (Hint: to find the documentation for a specific class, simply google search the class name, followed by java. For example, to find this documentation, I searched "BigDecimal Java")BigDecimal
at all unless you use it end-to-end. This requirement compromises accuracy; may compromise regulatory compliance; and may constitute actionable negligence.Double
and notdouble
. Is it a field which can benull
?double
is not that bad, but as it has 15+ digits of representation accuracy which is more than most programs need, but I agree there is no point using BigDecimal some of the time as you are more likely to add bugs/complexity.