math

Java BigDecimal Example

In this article we will learn about a primitive data type in java the BigDecimal class in java. We will briefly discuss about the various methods available in the BigDecimal class.

1. What is BigDecimal?

This primitive data type in java consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.

2. Constructors and methods of BigDecimal

2.1 Constructors

The different constructors available for BigDecimal class are shown below

2.1.1 BigDecimal(BigInteger value)

This accepts the BigInteger value and converts it into a BigDecimal value

2.1.2 BigDecimal(char[] value)

This accepts the character array representation of the BigDecimal value and converts it into a BigDecimal value

2.1.3 BigDecimal(String value)

This accepts the string representation of the BigDecimal value and converts it into a BigDecimal value

2.1.4 BigDecimal(int value)

This accepts an integer value and converts it into a BigDecimal value

2.1.5 BigDecimal(long value)

This accepts a long value and converts it into a BigDecimal value

2.1.6 BigDecimal(double value)

This accepts a double value and converts it into a BigDecimal value

2.1.7 BigDecimal(BigInteger unscaledValue, int scale)

This accepts a unscaled BigInteger value apply the scale and then converts it into a BigDecimal value

2.1.8 BigDecimal(BigInteger value, MathContext context)

This accepts the BigInteger value and apply the math context for rounding the value and then converts it into a BigDecimal value

2.1.9 BigDecimal(char[] value, MathContext context)

This accepts the character array representation of BigDecimal and apply the math context for rounding the value and then converts it into a BigDecimal value

2.1.10 BigDecimal(String value, MathContext context)

This accepts the string representation of BigDecimal and apply the math context for rounding the value and then converts it into a BigDecimal value

2.1.11 BigDecimal(int value, MathContext context)

This accepts the integer value and apply the math context for rounding the value and then converts it into a BigDecimal value

2.1.12 BigDecimal(long value, MathContext context)

This accepts the long value and apply the math context for rounding the value and then converts it into a BigDecimal value

2.1.13 BigDecimal(double value, MathContext context)

This accepts the double value and apply the math context for rounding the value and then converts it into a BigDecimal value

2.1.14 BigDecimal(BigInteger unscaledValue, int scale,MathContext context)

This accepts an unscaled BigInteger value, apply the scale and then apply the math context for rounding the value and converts it into a BigDecimal value

2.1.15 BigDecimal(char[] value, int offset, int length)

This accepts the character array representation of the BigDecimal value specifying the subarray to be converted and converts it into a BigDecimal value

2.1.16 BigDecimal(char[] value, int offset, int length, MathContext mc)

This accepts the character array representation of the BigDecimal value specifying the subarray to be converted and converts it into a BigDecimal value after rounding off based on the math context.

2.2 Methods of this primitive data type in Java

There are several methods available for performing various arithmetic task using the BigDecimal class. Some of them are shown below

2.2.1 The multiply method

BigDecimal multiply(BigDecimal value);

This method is used to multiply two BigDecimal number and returns the multiplication result as a BigDecimal number

2.2.2 The divide method

BigDecimal divide(BigDecimal value)

This method is used to divide two BigDecimal number and returns the division result as a BigDecimal number

2.2.3 The add method

BigDecimal add(BigDecimal value);

This method is used to add two BigDecimal number and returns the addition result as a BigDecimal number

2.2.4 The subtract method

BigDecimal subtract(BigDecimal value)

This method is used to subtract two BigDecimal number and returns the subtraction result as a BigDecimal number

2.2.5 The max method

BigDecimal max(BigDecimal value)

This method is used to find out the maximum of two BigDecimal number and returns the maximum of the two BigDecimal number

2.2.6 The min method

BigDecimal min(BigDecimal value);

This method is used to find out the minimum of two BigDecimal number and returns the minimum of the two BigDecimal number

2.2.7 The subtract method

BigDecimal subtract(BigDecimal value)

This method is used to subtract two BigDecimal number and returns the subtraction result as a BigDecimal number

2.2.8 The pow method

BigDecimal pow(int value);

This method is used to find out the BigDecimal value with the power of the integer value passed and return the result as a BigDecimal number

2.2.9 The round method

BigDecimal round(MathContext value)

This method is used to round off the BigDecimal number using the math context passed and returns the result as a BigDecimal number

2.2.10 The compareTo method

BigDecimal compareTo(BigDecimal value)

This method is used to compare two BigDecimal number and returns -1, 0 or 1 if this BigDecimal is smaller, equal or greater than the BigDecimal value passed respectively

3. Operations using Java BigDecimal class

java bigdecimal

The toString() method provides a canonical representation of a BigDecimal. Using a BigDecimal implies that you should:

  • Create new BigDecimal variables, using the constructor.
  • In order to add a BigDecimal to another BigDecimal, use add(BigDecimal augend) API method, that returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).
  • In order to multiply two BigDecimals, use multiply(BigDecimal multiplicand), that returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).
  • Use subtract(BigDecimal subtrahend) method to get a BigDecimal whose value is (this – subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).
  • Use divide(BigDecimal divisor) method to get a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() – divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.
  • Use pow(int n) method to get a BigDecimal whose value is the number raised to the n. The power is computed exactly, to unlimited precision.
  • Use negate() to get a BigDecimal whose value is (-this), and whose scale is this.scale().

Let’s take a look at the code snippet that follows:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.javacodegeeks.snippets.core;
 
import java.math.BigDecimal;
 
public class BigDecimalExample {
     
    public static void main(String[] args) {
         
        // Create two new BigDecimals
        BigDecimal BigDec1 = new BigDecimal("1238126387123");
        BigDecimal BigDec2 = new BigDecimal("1213669989183");
 
        // Addition of two BigDecimals
        BigDec1 = BigDec1.add(BigDec2);
        System.out.println("BigDec1 = " + BigDec1);
 
        // Multiplication of two BigDecimals
        BigDec1 = BigDec1.multiply(BigDec2);
        System.out.println("BigDec1 = " + BigDec1);
 
        // Subtraction of two BigDecimals
        BigDec1 = BigDec1.subtract(BigDec2);
        System.out.println("BigDec1 = " + BigDec1);
 
        // Division of two BigDecimals
        BigDec1 = BigDec1.divide(BigDec2);
        System.out.println("BigDec1 = " + BigDec1);
 
        // BigDecima1 raised to the power of 2
        BigDec1 = BigDec1.pow(2);
        System.out.println("BigDec1 = " + BigDec1);
 
        // Negate value of BigDecimal1
        BigDec1 = BigDec1.negate();
        System.out.println("BigDec1 = " + BigDec1);
    }
}

Output

BigDec1 = 2451796376306
BigDec1 = 2975671681510221617497998
BigDec1 = 2975671681509007947508815
BigDec1 = 2451796376305
BigDec1 = 6011305470862329165453025
BigDec1 = -6011305470862329165453025

 This was an example of how to use a BigDecimal in Java.

4. Summary

In this article we learned about the BigDecimal class in Java and how it helps dealing with big numbers. We discussed about various constructor and methods of the BigDecimal class. We also saw the various operations we can perform using the methods and looked into few example code.

5. Download the source code

Download
You can download the full source code of this example here: Java BigDecimal Example

Last updated on Oct. 06th, 2020

Shankar Mata

Shankar works as a Lead Software Engineer in a leading company and has developed several applications. He is one of the decision makers for the design and architecture for all the applications in his team. He along with his team develop and deploy cloud native services. He had been working in the field of Java and cloud technologies. He is also Amazon and Microsoft certified solution architect for AWS and Azure respectively. He has done his Masters in Computer science from University of New Mexico, US and Bachelors from GITAM University India. He is also experienced in Angular Framework and developed several applications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button