Java – How to Negate a BigDecimal in Java ?

Problem Statement

You need to negate the value of the BigDecimal datatype in your java program.

Solution

Use the negate method defined in the BigDecimal class as shown below.

import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) {

        System.out.println("Abundantcode.com Java Tutorials");

        BigDecimal input1 = new BigDecimal(10102);
        BigDecimal output = input1.negate();
        System.out.println(output);

    }
}


The output of the above program is

Abundantcode.com Java Tutorials

-10102

%d bloggers like this: