BigDecimal negate() Function in Java - GeeksforGeeks

04 Dec, 2018
The java.math.BigDecimal.negate() method returns a BigDecimal whose value is the negated value of the BigDecimal with which it is used.Syntax:public BigDecimal negate()Parameters: The method does not take any parameters .Return Value: This method returns the negative value of the BigDecimal object and whose scale is this.scale().Below programs will illustrate the use of java.math.BigDecimal.negate() function:Program 1 :// Java program to demonstrate negate() methodimport java.math.*;  public class GFG {     public static void main(String[] args) {        // Create a BigDecimal object      BigDecimal num;              // Assign value to num      num = new BigDecimal("4743");        System.out.println( "Negated value is " + num.negate() );   }}Output:Negated value is -4743
Program 2:// Java program to demonstrate negate() methodimport java.math.*;  public class GFG {     public static void main(String[] args) {        // Create a BigDecimal object      BigDecimal num;              // Assign value to num      num = new BigDecimal("-9674283517.97");        System.out.println( "Negated value is " + num.negate() );   }}Output:Negated value is 9674283517.97
The java.math.BigDecimal.negate(MathContext mc) method returns a BigDecimal whose value is the negated value of it, i.e. obtained by rounding off according to the precision settings specified by the object of MathContext class.Syntax:public BigDecimal negate(MathContext mc)Parameters: The method accepts only one parameter mc of MathContext class object which specifies the precision settings for rounding off the BigDecimal.Return Value: This method returns the negated value of the object which is rounded as per the precision settings.Exception: The method might throw ArithmeticException if the result obtained is not exact but the rounding mode is UNNECESSARY.Below programs will illustrate the use of java.math.BigDecimal.negate(MathContext mc) method:Program 1// Java program to demonstrate negate(MathContext mc) methodimport java.math.*;  public class GFG {      public static void main(String[] args)    {          // create 2 BigDecimal objects        BigDecimal num, negv;          MathContext mc = new MathContext(4); // 4 precision          // assign value to num        num = new BigDecimal("78.6714");          // assign negate value of num to negv using mc        negv = num.negate(mc);        System.out.println("Negated value, rounded to 4"+        " precision " + negv);    }}Output:Negated value, rounded to 4 precision -78.67
Program 2// Java program to demonstrate negate(MathContext mc) methodimport java.math.*;  public class GFG {      public static void main(String[] args)    {          // create 2 BigDecimal objects        BigDecimal num, negv;          MathContext mc = new MathContext(12); // 12 precision          // assign value to num        num = new BigDecimal("-178901456.68431");          // assign negate value of num to negv using mc        negv = num.negate(mc);        System.out.println("Negated value, rounded to 12 "+        "precision " + negv);    }}Output:Negated value, rounded to 12 precision 178901456.684
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#negate()My Personal Notes
arrow_drop_upSave
The java.math.BigDecimal.negate() method returns a BigDecimal whose value is the negated value of the BigDecimal with which it is used.Syntax:public BigDecimal negate()Parameters: The method does not take any parameters .Return Value: This method returns the negative value of the BigDecimal object and whose scale is this.scale().Below programs will illustrate the use of java.math.BigDecimal.negate() function:Program 1 :// Java program to demonstrate negate() methodimport java.math.*;  public class GFG {     public static void main(String[] args) {        // Create a BigDecimal object      BigDecimal num;              // Assign value to num      num = new BigDecimal("4743");        System.out.println( "Negated value is " + num.negate() );   }}Output:Negated value is -4743
Program 2:// Java program to demonstrate negate() methodimport java.math.*;  public class GFG {     public static void main(String[] args) {        // Create a BigDecimal object      BigDecimal num;              // Assign value to num      num = new BigDecimal("-9674283517.97");        System.out.println( "Negated value is " + num.negate() );   }}Output:Negated value is 9674283517.97

Syntax:
public BigDecimal negate()
Parameters: The method does not take any parameters .
Return Value: This method returns the negative value of the BigDecimal object and whose scale is this.scale().
Below programs will illustrate the use of java.math.BigDecimal.negate() function:Program 1 :
// Java program to demonstrate negate() methodimport java.math.*;  public class GFG {     public static void main(String[] args) {        // Create a BigDecimal object      BigDecimal num;              // Assign value to num      num = new BigDecimal("4743");        System.out.println( "Negated value is " + num.negate() );   }}
Negated value is -4743

Program 2:
// Java program to demonstrate negate() methodimport java.math.*;  public class GFG {     public static void main(String[] args) {        // Create a BigDecimal object      BigDecimal num;              // Assign value to num      num = new BigDecimal("-9674283517.97");        System.out.println( "Negated value is " + num.negate() );   }}
Negated value is 9674283517.97

The java.math.BigDecimal.negate(MathContext mc) method returns a BigDecimal whose value is the negated value of it, i.e. obtained by rounding off according to the precision settings specified by the object of MathContext class.Syntax:public BigDecimal negate(MathContext mc)Parameters: The method accepts only one parameter mc of MathContext class object which specifies the precision settings for rounding off the BigDecimal.Return Value: This method returns the negated value of the object which is rounded as per the precision settings.Exception: The method might throw ArithmeticException if the result obtained is not exact but the rounding mode is UNNECESSARY.Below programs will illustrate the use of java.math.BigDecimal.negate(MathContext mc) method:Program 1// Java program to demonstrate negate(MathContext mc) methodimport java.math.*;  public class GFG {      public static void main(String[] args)    {          // create 2 BigDecimal objects        BigDecimal num, negv;          MathContext mc = new MathContext(4); // 4 precision          // assign value to num        num = new BigDecimal("78.6714");          // assign negate value of num to negv using mc        negv = num.negate(mc);        System.out.println("Negated value, rounded to 4"+        " precision " + negv);    }}Output:Negated value, rounded to 4 precision -78.67
Program 2// Java program to demonstrate negate(MathContext mc) methodimport java.math.*;  public class GFG {      public static void main(String[] args)    {          // create 2 BigDecimal objects        BigDecimal num, negv;          MathContext mc = new MathContext(12); // 12 precision          // assign value to num        num = new BigDecimal("-178901456.68431");          // assign negate value of num to negv using mc        negv = num.negate(mc);        System.out.println("Negated value, rounded to 12 "+        "precision " + negv);    }}Output:Negated value, rounded to 12 precision 178901456.684
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#negate()My Personal Notes
arrow_drop_upSave
Syntax:
public BigDecimal negate(MathContext mc)
Parameters: The method accepts only one parameter mc of MathContext class object which specifies the precision settings for rounding off the BigDecimal.
Return Value: This method returns the negated value of the object which is rounded as per the precision settings.
Exception: The method might throw ArithmeticException if the result obtained is not exact but the rounding mode is UNNECESSARY.
Below programs will illustrate the use of java.math.BigDecimal.negate(MathContext mc) method:Program 1
// Java program to demonstrate negate(MathContext mc) methodimport java.math.*;  public class GFG {      public static void main(String[] args)    {          // create 2 BigDecimal objects        BigDecimal num, negv;          MathContext mc = new MathContext(4); // 4 precision          // assign value to num        num = new BigDecimal("78.6714");          // assign negate value of num to negv using mc        negv = num.negate(mc);        System.out.println("Negated value, rounded to 4"+        " precision " + negv);    }}
Negated value, rounded to 4 precision -78.67

Program 2
// Java program to demonstrate negate(MathContext mc) methodimport java.math.*;  public class GFG {      public static void main(String[] args)    {          // create 2 BigDecimal objects        BigDecimal num, negv;          MathContext mc = new MathContext(12); // 12 precision          // assign value to num        num = new BigDecimal("-178901456.68431");          // assign negate value of num to negv using mc        negv = num.negate(mc);        System.out.println("Negated value, rounded to 12 "+        "precision " + negv);    }}
Negated value, rounded to 12 precision 178901456.684

Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#negate()
Java-BigDecimal
Java-Functions
Java-lang package
java-math
Java-math-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
Stream In Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java