Read Also: Convert String to BigDecimal in Java
Convert BigInteger to BigDecimal in Java
1. Using BigDecimal's class constructor [Recommended]
You can easily convert BigInteger to BigDecimal using BigDecimal's class constructor.Syntax:
new BigDecimal(BigInteger val)
You can find the code below to convert BigInteger to BigDecimal in Java:
import java.math.BigInteger;
import java.math.BigDecimal;
public class BigIntegerToBigDecimal {
public static void main(String args[]) {
BigInteger bigInteger = new BigInteger("100000");
// Converting BigInteger to BigDecimal
BigDecimal bigDecimal = new BigDecimal(bigInteger);
System.out.println(bigDecimal);
}
}
Output:
100000
2. Using another BigDecimal's class constructor
Syntax:
new BigDecimal(BigInteger unscaledVal, int scale);
The above BigDecimal's class constructor translates a BigInteger unscaled value and an int scale into a BigDecimal.
Note: If you are using this constructor then keep the scale value to 0 as shown in the code below.
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigIntegerToBigDecimalTwo {
public static void main(String args[]) {
BigInteger bigInteger = new BigInteger("100");
// Converting BigInteger to BigDecimal
BigDecimal bigDecimal = new BigDecimal(bigInteger, 0);
System.out.println(bigDecimal);
}
}
Output:
100
That's all for today, please mention in the comments in case you know any other way of converting BigInteger to BigDecimal in Java.