How to Covert BigInteger to BigDecimal in Java

Covert BigInteger to BigDecimal

In this example you will learn about, How to Covert BigInteger to BigDecimal in Java. The BigInteger and BigDecimal both are belongs to java.math package.

Both classes are used to deals with large number digits in Java. This example will give Idea about How to covert BigInteger to BigDecimal in Java. The example were tested ans shared in the same article.

Conversion.java

package com.dineshkrish.math;

import java.math.BigDecimal;
import java.math.BigInteger;

public class Conversion {

	public static void main(String[] args) {
		
		// Defining BigInteger Object
		BigInteger bigInteger = new BigInteger("123456789101112131415");
		
		System.out.println("BigInteger");
		System.out.println("------------------");
		
		// Printing BigInteger
		System.out.println(bigInteger);
		
		// Defining BigDecimal Object passing BigInteger
		BigDecimal bigDecimal = new BigDecimal(bigInteger);
		
		System.out.println("\nBigDecimal");
		System.out.println("------------------");
		
		// Printing BigDecimal
		System.out.println(bigDecimal);
		
		System.out.println("\nConversion Done Successfully...");
	}
	
}

Output

BigInteger
——————
123456789101112131415

BigDecimal
——————
123456789101112131415

Conversion Done Successfully…

References

1. Java BigInteger API Documentation
2. Java BigDecimal API Documentation
3. Java BigDecimal Constrctor