Java BigDecimal Subtraction Tutorial

In Java, subtraction can be done using the subtract() method of BigDecimal. Here is an example code:

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal num1 = new BigDecimal("10.5");
        BigDecimal num2 = new BigDecimal("5.2");

        BigDecimal result = num1.subtract(num2);

        System.out.println("Subtraction result: " + result);
    }
}

In the example above, we initially create two BigDecimal objects num1 and num2. Next, we use the subtract() method to perform subtraction on them, storing the result in a new BigDecimal object named result. Finally, we print out the subtraction result.

Please note that since BigDecimal is an immutable object, the subtract() method will not modify the original BigDecimal object but will instead return a new BigDecimal object as the result.

bannerAds