1. Using valueOf() method [Recommended]
2. Using Constructor
Read Also: Convert String to BigDecimal in Java
Let's dive deep into the topic.
Convert Integer to BigDecimal in Java
1. Using valueOf() method [Recommended]
You can easily use BigDecimal's valueOf() method to convert Integer to BigDecimal.BigDecimal.valueOf(int);
Below is the complete example to convert Integer and primitive int to BigDecimal in Java using the valueOf() method.
import java.math.BigDecimal;
public class IntegerToBigDecimalOne {
public static void main(String args[]) {
// Converting int to BigDecimal
System.out.println("Converting int to BigDecimal");
int num = 100;
BigDecimal bd = BigDecimal.valueOf(num);
System.out.println(bd);
// Converting Integer to BigDecimal
System.out.println("Converting Integer to BigDecimal");
Integer num2 = 200;
BigDecimal bd2 = BigDecimal.valueOf(num2);
System.out.println(bd2);
}
}
When you run the above program, you will get the below Output:
Converting int to BigDecimal
100
Converting Integer to BigDecimal
200
2. Using Constructor
You can simply use BigDecimal's class constructor to convert Integer to BigDecimal. new BigDecimal(int);
Below is the complete example to convert primitive int and Integer to BigDecimal in Java using the constructor.
import java.math.BigDecimal;
public class IntegerToBigDecimalTwo {
public static void main(String args[]) {
// Converting int to BigDecimal
System.out.println("Converting int to BigDecimal");
int num = 300;
BigDecimal bd3 = new BigDecimal(num);
System.out.println(bd3);
// Converting Integer to BigDecimal
System.out.println("Converting Integer to BigDecimal");
Integer num2 = 400;
BigDecimal bd4 = new BigDecimal(num2);
System.out.println(bd4);
}
}
The Output will be:
Converting int to BigDecimal
300
Converting Integer to BigDecimal
400
That's it for today, please mention in the comments in case you know any other way of converting Integer to BigDecimal in Java.