Working with the BigDecimal class in JAVA

BigDecimal Class

The BigDecimal class is used to manipulate and round floating point numbers. It helps the programmer to obtain accurate decimal results in business applications requiring a higher degree of accuracy.  BigDecimal provides similar support for very large or very accurate floating point numbers. Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, which represents the number of digits to the right of the decimal point. The number represented by the BigDecimal.  BigDecimal provides operations for basic arithmetic, scale manipulation, comparison, hashing, and format conversion. It is defined in the library as:
extended byjava.lang.Number
extended byjava.math.BigDecimal 

Examples of BigDecimal Class

BigDecimal represents immutable, arbitrary-precision signed decimal numbers.
Constants for One, Ten and Zero.
  1. import java.math.BigDecimal;  
  2. public class Mainconstant   
  3.     {  
  4.     public static void main(String[] args) {  
  5.         System.out.println(BigDecimal.ONE);  
  6.         System.out.println(BigDecimal.TEN);  
  7.         System.out.println(BigDecimal.ZERO);  
  8.     }  




Rounding Mode

The java.math.RoundingMode enumeration specifies a rounding behavior for numerical operations capable of discarding precision. Each rounding mode indicates how the least significant returned digit of a rounded result is to be calculated.
This Enum provides various constants each of which is used for different modes of rounding. The decimal value would be rounded off to the number of decimal places based on the scale that is defined for the decimal value.
  1. import java.math.BigDecimal;   
  2. public class Round   
  3. {  
  4.     public static void main(String[] args)  
  5.    {  
  6.         BigDecimal first = new BigDecimal(1f);  
  7.         BigDecimal second = new BigDecimal(2f);  
  8.         BigDecimal result1 = first.divide(second, BigDecimal.ROUND_CEILING);  
  9.         BigDecimal result2 = first.divide(second, BigDecimal.ROUND_DOWN);  
  10.         BigDecimal result3 = first.divide(second, BigDecimal.ROUND_FLOOR);  
  11.         BigDecimal result4 = first.divide(second, BigDecimal.ROUND_HALF_DOWN);  
  12.         BigDecimal result5 = first.divide(second, BigDecimal.ROUND_HALF_EVEN);  
  13.         BigDecimal result6 = first.divide(second, BigDecimal.ROUND_HALF_UP);  
  14.         BigDecimal result7 = first.divide(second, BigDecimal.ROUND_UP);  
  15.         System.out.println(result1);  
  16.         System.out.println(result2);  
  17.         System.out.println(result3);  
  18.         System.out.println(result4);  
  19.         System.out.println(result5);  
  20.         System.out.println(result6);  
  21.         System.out.println(result7);  
  22.     }  
  23. }  

Create BigDecimals

To create a BigDecimal object in Java, you call one of the constructors. Each of these constructors takes a value and converts it to a BigDecimal object.
  1. import java.math.BigDecimal;  
  2. public class CreateBigDecimal 
  3.  {  
  4.     public static void main(String[] args)  
  5.    {  
  6.         System.out.println(new BigDecimal(1f));  
  7.         System.out.println(new BigDecimal(2f));  
  8.         System.out.println(new BigDecimal(3f));  
  9.         System.out.println(new BigDecimal(4f));  
  10.         System.out.println(new BigDecimal(5f));  
  11.         System.out.println(new BigDecimal(5f));  
  12.         System.out.println(new BigDecimal(6f));  
  13.     }  
  14. }  

Method use For Calculation

It is method used for calculation in BigDecimal class in Java.
  1. import java.math.BigDecimal;  
  3. public class Method   
  4. {  
  5.     public static void main(String[] args)   
  6.     {  
  7.         BigDecimal first = new BigDecimal(-5f);  
  8.         System.out.println(first);  
  9.         System.out.println(first.abs());  
  10.     }  


Compare Two Bigdecimal

It is method used for comparing two BigDecimal objects in Java.
  1. import java.math.BigDecimal;  
  2. public class Compare  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         BigDecimal first = new BigDecimal(10f);  
  7.         BigDecimal second = new BigDecimal(10f);  
  8.         System.out.println(first.compareTo(second));  
  9.         BigDecimal third = new BigDecimal(10f);  
  10.         BigDecimal fourth = new BigDecimal(20f);  
  11.         System.out.println(third.compareTo(fourth));  
  12.     }  


Move Decimal

This method is used to move the decimal point of the current BigDecimal by n places to the right.
  1. import java.math.BigDecimal;  
  2. public class Move  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         BigDecimal first = new BigDecimal(10000f);  
  7.         System.out.println(first);  
  8.         System.out.println(first.movePointLeft(1));  
  9.         System.out.println(first.movePointLeft(2));  
  10.         System.out.println(first.movePointLeft(3));  
  11.     }  

There are some another uses of Bigdecimal Class.

Remove the trailing zeroes, Convert double into long decimal, Scale and precision.


In this article, we learned about BigDecimal class in Java and how to use them in our Java programs.