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:
 
java.lang.Object
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.
 
Mainconstant.java
  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.     }  

Output
 
main-constant
 

 

 
 
 
 
 

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.
 
 java.lang.Object
        java.lang.Enum<RoundingMode>
            java.math.RoundingMode 
 
Round.java
  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. }  
Output
 
rounding-mode
 

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.
 
CreateBigDecimal.java
  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. }  
Output
 
create-bigdata
 

Method use For Calculation

 
It is method used for calculation in BigDecimal class in Java.
 
Method.java
  1. import java.math.BigDecimal;  
  2.   
  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.     }  

Output
 
use-for-calculation
 

Compare Two Bigdecimal

 
It is method used for comparing two BigDecimal objects in Java.
 
Compare.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.     }  

Output
 
compare
 

Move Decimal

 
This method is used to move the decimal point of the current BigDecimal by n places to the right.
 
Move.java
  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.     }  

Output
 
move
  
There are some another uses of Bigdecimal Class.

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

Summary

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


Similar Articles