Java Data Type Tutorial - Java BigDecimal .subtract (BigDecimal subtrahend, MathContext mc)








Syntax

BigDecimal.subtract(BigDecimal subtrahend, MathContext mc) has the following syntax.

public BigDecimal subtract(BigDecimal subtrahend,   MathContext mc)

Example

In the following code shows how to use BigDecimal.subtract(BigDecimal subtrahend, MathContext mc) method.

import java.math.BigDecimal;
import java.math.MathContext;
/*  w ww. ja  v a  2  s.c o  m*/
public class Main {

  public static void main(String[] args) {

    MathContext mc = new MathContext(2); // 2 precision

    BigDecimal bg1 = new BigDecimal("100.123");
    BigDecimal bg2 = new BigDecimal("50.56");

    // subtract bg1 with bg2 using mc and assign result to bg3
    BigDecimal bg3 = bg1.subtract(bg2, mc);

    String str = "The Result of Subtraction is " + bg3;

    // print bg3 value
    System.out.println(str);
  }
}

The code above generates the following result.