Java Convert double to BigDecimal
Tags: double BigDecimal
In this Java core tutorial we learn how to convert a double value to a java.math.BigDecimal object in Java programming language.
How to convert double to BigDecimal in Java
In Java, with a given double value we can use the BigDecimal.valueOf(double val) static method to instantiate a new BigDecimal object from a double value as the following example Java code.
ConvertDoubleToBigDecimalExample1.java
import java.math.BigDecimal;
public class ConvertDoubleToBigDecimalExample1 {
public static void main(String... args) {
double doubleValue = 200500.456;
// Convert double value to BigDecimal object
BigDecimal bigDecimalValue = BigDecimal.valueOf(doubleValue);
System.out.println("Double value: " + doubleValue);
System.out.println("BigDecimal value: " + bigDecimalValue);
}
}
Double value: 200500.456
BigDecimal value: 200500.456
Or we can use the BigDecimal(double val) constructor to create a new instance of BigDecimal from a given double value as the Java program below.
ConvertDoubleToBigDecimalExample2.java
import java.math.BigDecimal;
public class ConvertDoubleToBigDecimalExample2 {
public static void main(String... args) {
double doubleValue = 200500.456;
// Convert double value to BigDecimal object
BigDecimal bigDecimalValue = new BigDecimal(doubleValue);
System.out.println("Double value: " + doubleValue);
System.out.println("BigDecimal value: " + bigDecimalValue);
}
}
Double value: 200500.456
BigDecimal value: 200500.45600000000558793544769287109375
Happy Coding 😊
Related Articles
Java Convert long to BigDecimal
Java Convert int to BigDecimal
Java Convert float to BigDecimal
Java Convert BigInteger to BigDecimal
How to use BigDecimal in Java by Examples
How to Add two BigDecimal values in Java
How to Subtract two BigDecimal values in Java
How to Multiply two BigDecimal values in Java
How to Divide two BigDecimal values in Java
Java Convert BigDecimal value into double value
Java Convert BigDecimal value into float value
Java Convert BigDecimal value into int value
Java Convert BigDecimal value into long value
Java Convert BigDecimal value into short value
Java Convert BigDecimal value to byte value
Java Convert BigDecimal to String
Java Convert String to BigDecimal
Java Compare two BigDecimal values
How to Negate BigDecimal Value in Java
How to Round BigDecimal Value in Java