Java BigDecimal Class - w3resource
w3resource

Java BigDecimal Class

Introduction

Java includes a BigDecimal class for performing high-precision arithmetic which can be used in banking or financial domain based application. This class approximately fit into the same category as the “wrapper” classes but has some very useful methods.

This class has methods that provide analogs for the operations that you perform on primitive types. That is, you can do anything with a BigDecimal that you can with an int or float, it’s just that you must use method calls instead of operators. Also, since there’s more involved, the operations will be slower. You’re exchanging speed for accuracy.

BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate monetary calculations. Below Java code explains the concept of accuracy in calculation. One part is doing all calculation using double while another part is having calculation using BigDecimal. The output shows a difference between them.

Java Code: Go to the editor

import java.math.BigDecimal;
public class BigDecimalDemo {
	public static void main(String[] argv) {		 
		  System.out.println("--- Normal Print-----");
		  System.out.println(2.00 - 1.1);
		  System.out.println(2.00 - 1.2);
		  System.out.println(2.00 - 1.3);
		  System.out.println(2.00 - 1.4);
		  System.out.println(2.00 - 1.5);
		  System.out.println(2.00 - 1.6);
		  System.out.println(2.00 - 1.7);
		  System.out.println("--- BigDecimal Usage Print-----");
		  System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.1")));
		  System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.2")));
		  System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.3")));
		  System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.4")));
		  System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.5")));
		  System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.6")));
		  System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.7")));	
		  BigDecimal bd1 = new BigDecimal ("1234.34567");
		 bd1= bd1.setScale (3, BigDecimal.ROUND_CEILING);     
		  System.out.println(bd1);
	}
}

Output:

java bigdecimal image1

BigDecimal constructors

BigDecimal important methods

Limiting a number to a certain degree of precision

Let us say, we would like to calculate something, up to 3 decimal places, no more and no less, and independent of the number we used to instantiate the BigDecimal. In that case, we will have to specify two properties of a BigDecimal object. The rounding mode and the scale. The scale defines the number of decimal places we want, and the rounding mode specifies how we would like to round a floating point number. As an example, we will modify the task above as follows :

"After you have created the program, your bosses at NASA have changed their minds, and they now want the result accurate up to no more than 3 decimal places. You are now required to change your program."

BigDecimal bd1 = new BigDecimal ("1234.34567");
bd1= bd1.setScale (3, BigDecimal.ROUND_CEILING);     
System.out.println(bd1);    

Output would be 1234.346

Comparing BigDecimals

As with primitive numeric data types, one will sometimes need to compare BigDecimals with each other. To do so, one will have to use the "x.compareTo( BigDecimal bd)" method. (Note: Do not use the .equals() method. It is a common mistake to do so, as it compares two pointers to determine of they point to the same object, and has nothing to do with numeric equality).

The compareTo method works as follows: let's say we make the call "x.compareTo(y)". The result of this call will be : (as in the comparable interface)

-1 : if x < y
 0 : if x == y 
+1: if x > y    

Java Code: Go to the editor

import java.math.BigDecimal;
public class BigDecimalCompare {
	 public static void main(String[]args){
         BigDecimal a = new BigDecimal("31234"); 
         BigDecimal b = new BigDecimal("31234.00");
         BigDecimal c = new BigDecimal("-21234");
         System.out.println("a compared to b = "+a.compareTo(b)); 
         System.out.println("a compared to c = "+a.compareTo(c)); 
         System.out.println("b compared to a = "+b.compareTo(a)); 
         System.out.println("b compared to c = "+b.compareTo(c));
         System.out.println("c compared to a = "+c.compareTo(a)); 
         System.out.println("c compared to b = "+c.compareTo(b)); 
        
    }
}

Output:

java bigdecimal image2

Summary:

  • The BigDecimal class should be used when we need accuracy and particular scale in our results.
  • BigDecimal is similar to other wrapper classes having specific methods for addition, subtraction, multiplication, and division.
  • This wrapper class operations are slightly slower compared to primitive types.

Java Code Editor:

Previous:Garbage Collection in Java



Follow us on Facebook and Twitter for latest update.