java.math → Class BigDecimal

The The BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating point numbers with great precision but compensating with the time complexity a bit.
A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point. If less than zero, the unscaled value of the number is multiplied by 10^(-scale). provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating point numbers with great precision but compensating with the time complexity a bit.

A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point. If less than zero, the unscaled value of the number is multiplied by 10^(-scale).

import java.math.BigDecimal; 
public class BigDecimalExample 
{ 
    public static void main(String[] args)  
    { 
        // Create two new BigDecimals 
        BigDecimal bd1 =  
               new BigDecimal("12411567890.0987654321"); 
        BigDecimal bd2 =  
               new BigDecimal("9876114321.123456789"); 
          
        // Addition of two BigDecimals 
        bd1 = bd1.add(bd2); 
        System.out.println("BigDecimal1 = " + bd1); 
  
        // Multiplication of two BigDecimals 
        bd1 = bd1.multiply(bd2); 
        System.out.println("BigDecimal1 = " + bd1); 
  
        // Subtraction of two BigDecimals 
        bd1 = bd1.subtract(bd2); 
        System.out.println("BigDecimal1 = " + bd1); 
  
        // Division of two BigDecimals 
        bd1 = bd1.divide(bd2); 
        System.out.println("BigDecimal1 = " + bd1); 
  
        // BigDecima1 raised to the power of 2 
        bd1 = bd1.pow(2); 
        System.out.println("BigDecimal1 = " + bd1); 
  
        // Negate value of BigDecimal1 
        bd1 = bd1.negate(); 
        System.out.println("BigDecimal1 = " + bd1); 
    }     
}

Extraction of value from BigDecimal

double x = A.doubleValue(); 
String z = A.toString();

BigDecimal Sum Using Map

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
public class BigDecimalSumUsingMap {
  public static void main(String[] args) {
    Map<Integer, BigDecimal> map = new HashMap<>();
    map.put(1, new BigDecimal("45.23"));
    map.put(2, new BigDecimal("55.43"));
    map.put(3, new BigDecimal("65.21"));
    map.put(4, new BigDecimal("35.73"));
    
    BigDecimal sum = map.values().stream().reduce(BigDecimal.ZERO, (p, q) -> p.add(q));
    System.out.println(sum);
    sum = map.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
    System.out.println(sum);
    sum = map.values().stream().reduce(BigDecimal.ZERO, Utility::addWeight);
    System.out.println(sum);			
  }

}

THIS IS THE MOST COMMON MISTAKE MADE WITH BIGDECIMALS!

Comparison

It is important to never use the .equals() method to compare BigDecimals. That is because this equals function will compare the scale. If the scale is different, .equals() will return false, even if they are the same number mathematically.

BigDecimal a = new BigDecimal("2.00"); 
BigDecimal b = new BigDecimal("2.0"); 
print(a.equals(b)); // false

Instead, we should use the .compareTo() and .signum()methods.

a.compareTo(b); // returns (-1 if a < b), (0 if a == b), (1 if a > b) 
a.signum(); // returns (-1 if a < 0), (0 if a == 0), (1 if a > 0)

Links

  1. https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html