Home » Java programming language

BigInteger Class and BigDecimal Class in Java

Learn: BigInteger Class and BigDecimal Class in Java. This article will explain you about the use the BigInteger Class and BigDecimal Class and how these classes are reducing the problem of handling Large Numbers.
Submitted by Mayank Singh, on June 13, 2017

In real life we frequently come across numbers, sometimes small but sometimes large, while in programming primitive data types have a disadvantage that they can handle numbers only up to a unique range which is small, but to fulfil our requirement Java introduced BigInteger and BigDecimal Classes, these Classes can handle numbers up to a very long range as they handle numbers dynamically unlike primitive data types.

First let us discuss BigInteger Class:

BigInteger Class

This Class can be imported by the following statement: import java.math.BigInteger;

As seen above BigInteger Class is the part of math Package.

Consider the program:

import java.util.Scanner;
import java.math.BigInteger;

class BigIntegerExample
{
	public static void main(String args[])
	{
		Scanner KB=new Scanner(System.in);
		String a,b;
		System.out.println("Enter First Number :");
		a=KB.nextLine();
		System.out.println("Enter Second Number :");
		b=KB.nextLine();
		BigInteger A=new BigInteger(a);
		BigInteger B=new BigInteger(b);
		BigInteger Sum=A.add(B);
		BigInteger Multiplied=A.multiply(B);
		BigInteger Remain=A.remainder(B);
		System.out.println("Sum Of the two Numbers is :"+Sum);
		System.out.println("Multiplication of the two Numbers is :"+Multiplied);
		System.out.println("Remainder when "+A+"is divided by "+B+" :"+Remain);
	}
}

Output

Enter First Number :
142536141253654546456787823232356421653594544646585265116519
Enter Second Number :
556554655656654655556
Sum Of the two Numbers is :142536141253654546456787823232356421654151099302241919772075
Multiplication of the two Numbers is :79329153014055994331638907597080521393268861920828184819191877862617726150729564
Remainder when 142536141253654546456787823232356421653594544646585265116519is divided by 556554655656654655556 :79576899974494740771

As we know,
Strings in java are also dynamic in nature, what we are actually doing in the above program is that we first take the input of the number in a String format,then we convert String to BigInteger format by passing String as a parameter in the following Statement.

BigIntegerObjectName=new BigInteger(String);

Also, there are some default BigInteger declarations like:

BigInteger.ZERO; // A BigInteger Constant equal to 0
BigInteger.ONE;// A BigInteger Constant equal to 1
BigInteger.TEN;// A BigInteger Constant equal to 10

Methods used in BigInteger Class:

Method Primitive Data Type (int a,b,c) BigInteger Class (BigInteger A,B,C)
Addition c=a+b C=A.add(B)
Multiplication c=a*b C=A.multiply(B)
Remainder c=a%b C=A.remainder(B)
Subtraction c=a-b C=A.subtract(B)

To know more about the methods available in the BigInteger Class follow this link to Javadoc: LINK


BigDecimal Class

Now Let us discuss the simple use of BigDecimal Class in Java, which is used to handle large Decimal Numbers, this is an actually useful class if you are dealing with some real-life numbers.

Consider the program:

import java.util.Scanner;
import java.math.BigDecimal;
import java.math.RoundingMode;

class BigDecimalExample
{
	public static void main(String args[])
	{
		Scanner KB=new Scanner(System.in);
		String a,b;
		System.out.println("Enter First Number :");
		a=KB.nextLine();
		System.out.println("Enter Second Number :");
		b=KB.nextLine();
		BigDecimal A=new BigDecimal(a);
		BigDecimal B=new BigDecimal(b);
		BigDecimal Divided=A.divide(B,6,RoundingMode.CEILING);
		System.out.println("Division when "+A+" is divided by "+B+" :"+Divided);
	}
}

Output

Enter First Number :
4555616565565656526259652699
Enter Second Number :
62566555645657
Division when 4555616565565656526259652699 is divided by 62566555645657 :72812327905122.270645

This Class can be imported by the following statement: import java.math.BigDecimal;

We are also importing the RoundingModeClass, in order to handle precision and scale of our numbers.

import java.math.RoundingMode;

As seen above BigDecimal Class and RoundingMode class are also the part of math Package.

General Parameters in BigDecimal Method:

BigDecimaldivide(BigDecimal divisor, int scale, RoundingModeroundingMode)

Scale: Total number of Digits after Decimal Point
RoundingMode: FLOOR or CEILING etc.

To know more about the other methods available in the BigDecimal Class follow this link to Javadoc: LINK



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.