Read Also: Convert BigInteger to BigDecimal in Java
Java Convert BigDecimal to Double
1. Using BigDecimal class doubleValue() method
You can easily convert BigDecimal to Double in Java using BigDecimal class doubleValue() method as shown below in the example. import java.math.BigDecimal;
public class BigDecimalToDouble {
public static void main(String args[]) {
// Creating BigDecimal object using Constructor
BigDecimal bd = new BigDecimal("3456.78912");
// Converting BigDecimal to Double using doubleValue()
Double doubleObj = bd.doubleValue();
// Printing Double
System.out.println("Converted BigDecimal to Double: " + doubleObj);
}
}
Output:
Converted BigDecimal to Double: 3456.78912
Converting BigDecimal to Double with 2 Decimal places
After converting BigDecimal to Double, you can easily restrict the Double to 2 decimal places by using BigDecimal class setScale() method as shown below in the example.
Note: Just like String, BigDecimal objects are immutable. Calls of setScale(int newScale, RoundingMode roundingMode) method do not result in the original object being modified.
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalToDouble2 {
public static void main(String args[]) {
// Instantiating BigDecimal using Constructor
BigDecimal bd = new BigDecimal("456.789123");
// Setting Decimal places to 2 using setScale() method
bd = bd.setScale(2, RoundingMode.HALF_DOWN);
// Converting BigDecimal to Double with 2 Decimal places
Double doubleObj = bd.doubleValue();
// Displaying Double
System.out.println("Converted BigDecimal to Double: " + doubleObj);
}
}
Output:
Converted BigDecimal to Double: 456.79
That's all for today, please mention in the comments in case you know any other way of converting BigDecimal to Double in Java.