{

How to Convert BigDecimal to Double or Double to BigDecimal in java with examples


BigDecimal is a big integer with arbitrary-precision signed decimal numbers.

It contains a 32-bit integer and an unscaled decimal value. It is defined in java. math class. It is used in product prices and scientific calculations

Double is used to store floating-point numbers and uses 64 bytes.

Bigdecimal to/from  double in java with examples

This tutorial shows you how to convert BigDecimal to double in java.

Note: If you are using currency values BigDecimal and converting to double, You lose precision and It gives wrong results in currency processing. You can also check my previous posts on the BigInteger class in java.

How to convert bigdecimal to double

BigDecimal has a method doubleValue that returns the double value.

It returns Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY based on how big is value in BigDecimal.

Here is an syntax ```markup public double doubleValue()


Here is an example program code

```java
import java.math.BigDecimal;

public class BigDecimalTest {
    public static void main(String[] args) {
        BigDecimal order = new BigDecimal(123.56789);
        System.out.println(order); //567890000000005557012627832591533660888671875
        System.out.println(order.getClass()); // class java.lang.BigDecimal
        Double d = order.doubleValue();
        System.out.println(d); //123.56789
        System.out.println(d.getClass()); //class java.lang.Double

    }
}

Output:

123.567890000000005557012627832591533660888671875
class java.math.BigDecimal
123.56789
class java.lang.Double

How to Convert Double to BigDecimal in java?

This section shows you ways to convert double to BigDecimal in java We can do it in two ways

  • BigDecimal Constructor: The constructor accepts Double values and returns BigDecimal Object.

        public BigDecimal(double value) 
    
  • valueOf method: The static method takes the double value and returns BigDecimal Object.

        public static BigDecimal valueOf(double val) 
    

Here is a complete program code

import java.math.BigDecimal;

public class BigDecimalTest {
    public static void main(String[] args) {

        Double d = 123.56789;
        System.out.println(d); //123.56789
        System.out.println(d.getClass()); //class java.lang.Double


        // using constructor
        BigDecimal order = new BigDecimal(d);
        System.out.println(order); //123.67890000000005557012627832591533660888671875
        System.out.println(order.getClass()); //class java.lang.BigDecimal
        
        // using valueOf method
        BigDecimal order1 = BigDecimal.valueOf(d);
        System.out.println(order1); //123.56789
        System.out.println(order1.getClass()); //class java.lang.BigDecimal


    }
}

Output:

123.56789
class java.lang.Double
123.567890000000005557012627832591533660888671875
class java.math.BigDecimal
123.56789
class java.math.BigDecimal

Summary

Learned multiple ways to Convert BigDecimal to or from double in java. Also, converting bigdecimal to double result loss of precision values and try to avoid in currency calculations.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.