BigDecimal calculation

In this chapter you will learn:

  1. What methods to use to do calculation on BigDecimal
  2. How to get absolute value from BigDecimal
  3. How to add two BigDecimal value together
  4. Calculating Euler's number e with BigDecimal
  5. Multiply one BigDecimal to another BigDecimal

Methods to do calculation

BigDecimal  abs()/*from   ja  v  a 2 s.  co m*/
    BigDecimal  abs(MathContext mc)
    BigDecimal  add(BigDecimal augend)
    BigDecimal  add(BigDecimal augend, MathContext mc)
    BigDecimal  divide(BigDecimal divisor)
    BigDecimal  divide(BigDecimal divisor, int roundingMode)
    BigDecimal  divide(BigDecimal divisor, int scale, int roundingMode)
    BigDecimal  divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
    BigDecimal  divide(BigDecimal divisor, MathContext mc)
    BigDecimal  divide(BigDecimal divisor, RoundingMode roundingMode)
    BigDecimal[]divideAndRemainder(BigDecimal divisor)
    BigDecimal[]divideAndRemainder(BigDecimal divisor, MathContext mc)
    BigDecimal  divideToIntegralValue(BigDecimal divisor)
    BigDecimal  divideToIntegralValue(BigDecimal divisor, MathContext mc)
    BigDecimal  max(BigDecimal val)
    BigDecimal  min(BigDecimal val)
    BigDecimal  multiply(BigDecimal multiplicand)
    BigDecimal  multiply(BigDecimal multiplicand, MathContext mc)
    BigDecimal  negate()
    BigDecimal  negate(MathContext mc)
    BigDecimal  plus()
    BigDecimal  plus(MathContext mc)
    BigDecimal  pow(int n)
    BigDecimal  pow(int n, MathContext mc)
    BigDecimal  remainder(BigDecimal divisor)
    BigDecimal  remainder(BigDecimal divisor, MathContext mc)
    BigDecimal  round(MathContext mc)
    BigDecimal  scaleByPowerOfTen(int n)
    BigDecimal  subtract(BigDecimal subtrahend)
    BigDecimal  subtract(BigDecimal subtrahend, MathContext mc)

Absolute value

The following code calls abs() method to return the absolute value from a BigDecimal.

import java.math.BigDecimal;
 /* jav  a  2  s  . c om*/

public class Main {
 
    public static void main(String[] args) {
        BigDecimal first = new BigDecimal(-1f);
        System.out.println(first);
        System.out.println(first.abs());
    }
}

The output:

Add two BigDecimal value together

We can use add() method to add one BigDecimal value to another.

import java.math.BigDecimal;
 //from   j  av a  2s .  co m

public class Main {
 
    public static void main(String[] args) {
        BigDecimal first = new BigDecimal(-1f);
        BigDecimal second = new BigDecimal(10f);
 
        BigDecimal result = first.add(second); 
        System.out.println(result);
    }
}

The output:

Calculating Euler's number e with BigDecimal

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
//from  j a  v a2s .  co  m
public class Main {
  final static int LASTITER = 17;

  public static void main(String[] args) {
    MathContext mc = new MathContext(100, RoundingMode.HALF_UP);
    BigDecimal result = BigDecimal.ZERO;
    for (int i = 0; i <= LASTITER; i++) {
      BigDecimal factorial = factorial(new BigDecimal(i));
      BigDecimal res = BigDecimal.ONE.divide(factorial, mc);
      result = result.add(res);
    }
    System.out.println(result);
  }

  public static BigDecimal factorial(BigDecimal n) {
    if (n.equals(BigDecimal.ZERO))
      return BigDecimal.ONE;
    else
      return n.multiply(factorial(n.subtract(BigDecimal.ONE)));
  }

}

The code above generates the following result.

Multiply one BigDecimal to another BigDecimal

import java.math.BigDecimal;
//  ja  v  a 2 s  .  co  m
public class Main {
  public static void main(String[] argv) throws Exception {
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);

    bd1 = bd1.multiply(bd2);
    
    System.out.println(bd1);
  }
}

Output:

Next chapter...

What you will learn in the next chapter:

  1. How to convert BigDecimal to primitive data types
  2. How to convert to float type value
  3. Convert double and long to BigDecimal
Home » Java Tutorial » BigDecimal BigInteger

BigDecimal

    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format

BigInteger

    BigInteger class
    BigInteger creation
    BigInteger add, subtract, multiply and divide
    BigInteger power and modPow
    BigInteger conversion
    BigInteger to String
    BigInteger bit and,or,xor,test,flip,negate
    BigInteger bit shift left and right
    BigInteger prime value
    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format