Subtract one BigDecimal from another BigDecimal in Java

Java 8Object Oriented ProgrammingProgramming

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 −


 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);


Value 1 : 375789755.345778656
Value 2 : 625678755.155778656
Result (Subtraction) = 249888999.810000000
Updated on 30-Jul-2019 22:30:24