167

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.

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • 16
    How about `new BigDecimal(2).doubleValue()` – Matthias Oct 29 '13 at 06:08
  • 6
    Always Read the [documentation](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) before asking a question. You will often find that your question is answered. See [`doubleValue()`](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#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") – Justin Oct 29 '13 at 06:09
  • 1
    "I have a requirement where we have to use Double as argument" Get the requirement changed. There's no point in using `BigDecimal` at all unless you use it end-to-end. This requirement compromises accuracy; may compromise regulatory compliance; and may constitute actionable negligence. – user207421 Oct 29 '13 at 06:30
  • Why are you using `Double` and not `double`. Is it a field which can be `null`? – Peter Lawrey Oct 29 '13 at 06:59
  • @EJP Using `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. – Peter Lawrey Oct 29 '13 at 07:01
  • @PeterLawrey If he has a requirement to use `BigDecimal,` he has an implicit or explicit collateral requirement not to use double for the same data. – user207421 Oct 29 '13 at 07:49
  • @EJP I agree, If you are using BigDecimal, there should be a good reason for it. – Peter Lawrey Oct 29 '13 at 07:55

3 Answers3

316

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
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • 2
    Hi, 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 – CODAR747 Aug 02 '20 at 05:57
  • 1
    @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](https://chortle.ccsu.edu/java5/Notes/chap11/ch11_5.html). – Rahul Aug 04 '20 at 08:46
  • hi, it worked for me... straightforward solution – Mario Rojas Dec 09 '22 at 19:18
40

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.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
HIREN011
  • 549
  • 1
  • 7
  • 7
13

Use doubleValue method present in BigDecimal class :

double doubleValue()

Converts this BigDecimal to a double.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136