Last Updated:

BigDecimal and BigInteger classes in Java

As a rule, a Java program consists of methods and variables, and the latter are divided into 2 types - integers and "real numbers". Their difference lies only in the contained values. The long variable takes the greatest meaning for the class of integers - from -9223372036854775808 to 9223372036854775807.

In the case of "real numbers", the highest indicator has double, which ranges from 1.7e-308 to 1.7e + 308. However, what should be done when you want to store numbers that are larger than these values? To do this, you will need to use BigInteger and BigDecimal, which will be discussed in this article.

JavaLanguage- Description of the BigDecimal and BigInteger packages

First you should deal with BigInteger. This class has no limit of 64 bits, has an arbitrary length. This component of the code is the heir of Number, as well as whole permenes like Integer, Double and the rest. Therefore, this class can implement methods that result in simple types:

BigInteger value = new BigInteger(«32145»);

intintValue = value.intValue();//32145

long longValue = value.longValue();//32145

double doubleValue = value.doubleValue();//32145.0

 

The constructor of the BigInteger class is far from one. And therefore, you can apply it for different cases. If it is easier to say - "For all occasions", since it is possible that any scenario will arise.

 

Sometimes it may happen that simple types are not able to save all the data from BigInteger, which will cause a reduction in the range. And the maximum value that is available for the primitive type will be taken.

There are also analogues of methods for work. Their difference lies in the actions with the type - if it does not contain the full range of information with the BigInteger, then an ArithmeticException is thrown.

Regarding the constants for BigInteger - it is worth highlighting several of them:

BigInteger.ZERO
BigInteger.ONE
BigInteger.TEN

Their meanings are already known from the names. That is, BigInteger.ZERO – 0, BigInteger.ONE – 1, and BigInteger.TEN – 10.

Basic information on the first class is sorted out. So, it is worth moving on to the study of BigDecimal. Here, as the name already shows, there is something in common. And what is it - the user himself will understand after reading the material further.

BigDecimal is already used to replace double. As a rule, the use of such a class is necessary when working with financial data. By analogy with BigInteger, BigDecimal is used for a real number of arbitrary length.

BigDecimal is also the successor to Number. Methods are returned as a specific primitive form. It looks so simple in the console:

BigDecimalvalue=newBigDecimal(35563.3);

LonglongValue=value.longValue();//35563

DoubledoubleValue=value.doubleValue();//35563.3

After a comma, a string with Long is followed by all characters. Therefore, the program will begin to count the whole value. This is what is stated in the description and belonging to Longk whole variables.

In the case of BigDecimal constructors, everything is more diverse. You can use different tools, for example, to set the value of an object. Alternatively, prepare the parameters of the created element. There are many of them, but they are all basic.

Here's the most basic version of the code:

BigDecimalfirstValue = newBigDecimal(«455656.545»);//455656.545

The number of decimal places (periods) has been set here. This, as is already clear, determines the possibility of a more accurate display.

BigDecimalsecondValue = new BigDecimal(3445.54);//3445.5399999999999636202119290828704833984375

It is also recommended to use String in the design. And everything is due to the fact that Double is an ambiguous data type. So, accuracy can not be expected.

BigDecimalthirdValue = newBigDecimal(3445.554645675444, MathContext.DECIMAL32);//3445.555

When setting Double, you also need to set rounding. This is necessary to avoid confusing values, if necessary.

char[] arr = new String(«455656.545»).toCharArray();

BigDecimalfourthValue = new BigDecimal(arr, 2, 6);//5656.5

Next, an array of characters is specified, from which information for the object is delivered, as well as how many of them are taken.

BigDecimalfifthValue = new BigDecimal(new BigInteger(«44554»), 3);//44.554

It remains only to take the already existing BigInteger class to set the number of decimal places.

BigInteger and BigDecimal Classes - Detailed Analysis (Methods)

To understand both classes in more detail, it is worth starting to study their methods of work. For each of them, there are enough to work without difficulties. Here's what a person can resort to when interacting with BigInteger:

BigIntegerfirstValue = new BigInteger(«37995»);
BigIntegersecondValue = new BigInteger(«35466»);
BigIntegerresultValue= firstValue.add(secondValue);//73461

These are summation operations.

BigIntegerfirstValue = new BigInteger(«37995»);
BigIntegersecondValue = new BigInteger(«35466»);
BigIntegerresultValue= firstValue.multiply(secondValue);//1347530670

Such actions are necessary for multiplication.

BigIntegerfirstValue = new BigInteger(«37995»);
BigIntegersecondValue = new BigInteger(«35466»);
BigIntegerresultValue= firstValue.remainder(secondValue);//2529

With the help of such a code, it is possible to calculate the remainder when dividing the numbers by each other.

BigIntegerfirstValue = new BigInteger(«-37995»);
BigIntegerresultValue= firstValue.abs();//37995

With such commands, it is possible to obtain a modular number. It doesn't have a sign. Now it's worth moving on to more complex operations. There are several of them and it may take longer to study. However, in some situations, they will help well. Here is a list of them:

BigIntegerfirstValue = new BigInteger(«-34»);
BigIntegersecondValue = new BigInteger(«5»);
BigIntegerresultValue = firstValue.mod(secondValue); 1

This piece of code allows you to calculate the mod. Also, there are variations of this function. Here they are:

BigIntegerfirstValue = BigInteger.probablePrime(8, new Random());//211
BigIntegersecondValue = BigInteger.probablePrime(16, new Random());//42571

This will allow you to get a random number with a set of bitness of the result. Also, BigInteger has bitwise shift operations. Here are both options:

BigIntegerfirstValue = new BigInteger(«5»);
BigIntegerfirstResultValue = firstValue.shiftLeft(3);//40

This is responsible for shifting to the left.

BigIntegersecondValue = new BigInteger(«34»);
BigIntegersecondResultValue = secondValue.shiftRight(2); 8

And so it is possible to make a shift to the right side. Now let's move on to the methods with another class, BigDecimal. Its functions differ from the same BigInteger in that they do not allow you to adjust the bitness, and additionally allow you to configure the "floating" point of the number. Here's what the user can use:

BigDecimal value = new BigDecimal(«454334.34334»);
int result = value.precision();//11

So you can get accuracy (the number of numbers);

BigDecimalfirstValue = new BigDecimal(3445.544445);

BigDecimalsecondValue = firstValue.setScale(3,BigDecimal.ROUND_CEILING);//3445.545

By setting such values, a person gets the opportunity to round and separate numbers before and after a comma.

This is how you divide one BigDecimal by another BigDecimal, and also set the number of numbers after the decimal point and the rule for rounding the resulting value:

BigDecimalfirstValue = new BigDecimal(«455656.545»);

BigDecimalsecondValue = new BigDecimal(3445.544445);

BigDecimal result = firstValue.divide(secondValue, 2,RoundingMode.DOWN);//132.24

This moves the comma by a certain number of digits to the right or left:

BigDecimal value = new BigDecimal(«455656.545»);
BigDecimalfirstResult = value.movePointRight (2);//45565654.5
BigDecimalsecondResult = value.movePointLeft (2);//4556.56545

With this command, you can remove the remaining zeros at the end:

BigDecimal value = new BigDecimal(«45056.5000»);
BigDecimal result = value.stripTrailingZeros();//45056.5

Thanks to such methods, work with both classes is performed. A person can easily interact with them and easily cope with various operations. And all the material on both classes will help in working with code with BigInteger and BigDecimal.