Subtract one BigDecimal from another BigDecimal in Java


Use the subtract method to subtract one BigDecimal to another in Java. TheBigDecimal.subtract(BigDecimal val) returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()). Here, “val” is the value to be subtracted from this BigDecimal.

The following is an example −

Example

 Live Demo

import java.math.BigDecimal;
public class Demo {
   public static void main(String[] argv) throws Exception {
      BigDecimal val1 = new BigDecimal("375789755.345778656");
      BigDecimal val2 = new BigDecimal("625678755.155778656");
      System.out.println("Value 1 : "+val1);
      System.out.println("Value 2 : "+val2);
      // subtract
      val2 = val2.subtract(val1);
      System.out.println("Result (Subtraction) = "+val2);
   }
}

Output

Value 1 : 375789755.345778656
Value 2 : 625678755.155778656
Result (Subtraction) = 249888999.810000000

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements