Java.math.BigDecimal.subtract() Method



Description

The java.math.BigDecimal.subtract(BigDecimal subtrahend) returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).

Declaration

Following is the declaration for java.math.BigDecimal.subtract() method.

public BigDecimal subtract(BigDecimal subtrahend)

Parameters

subtrahend − value to be subtracted from this BigDecimal.

Return Value

This method returns the value of BigDecimal object after subtraction with the subtrahend i.e, this - subtrahend.

Exception

NA

Example

The following example shows the usage of math.BigDecimal.subtract() method.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 3 BigDecimal objects
      BigDecimal bg1, bg2, bg3;

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

      // subtract bg1 with bg2 and assign result to bg3
      bg3 = bg1.subtract(bg2);

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

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

Let us compile and run the above program, this will produce the following result −

The Result of Subtraction is 49.563
java_math_bigdecimal.htm
Advertisements