Syntax error Subtract one BigDecimal from another BigDecimal in Java

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
Updated on: 2019-07-30T22:30:24+05:30

515 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements