Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaBigDecimal plus() method in Java

BigDecimal plus() method in Java

  1. The java.math.BigDecimal.plus() is an inbuilt method in java that returns a BigDecimal whose value is (+this), and whose scale is this.scale(). This method, which simply returns this BigDecimal is included for symmetry with the unary minus method negate().
    Syntax: 
     
public BigDecimal plus()
  1. Parameters: The function does not accepts any parameter.
    Return value: This method returns the object value i.e., this.
    Below program illustrates the working of the above mentioned method: 
    Program 1: 
     

Java




// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class Gfg {
 
    public static void main(String[] args)
    {
 
        // Assign value to b1
        BigDecimal b1 = new BigDecimal("-45.652");
 
        // Assign the result of plus method on
        // BigDecimal Objects b1 to b2
        BigDecimal b2 = b1.plus();
 
        // Print the value of b2
        System.out.println("The value of the BigDecimal is " + b2);
    }
}


  1.  
Output: 

The value of the BigDecimal is -45.652

 

  1. Program 2: 
     

Java




// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        // Assign value to b1
        BigDecimal b1 = new BigDecimal("7458.3256");
 
        // Assign the result of plus method on
        // BigDecimal Objects b1 to b2
        BigDecimal b2 = b1.plus();
 
        // Print the value of b2
        System.out.println("The value of the BigDecimal is " + b2);
    }
}


  1.  
Output: 

The value of the BigDecimal is 7458.3256

 

  1.  
  2. The java.math.BigDecimal.plus(MathContext mc) is an inbuilt method in java that returns a BigDecimal whose value is (+this), with rounding according to the context settings.
    Syntax: 
     
public BigDecimal plus(MathContext mc)
  1. Parameters: This method accepts a single parameter mc which refers to the context of rounding to use i.e., up to what digit the value would be rounded.
    Return value: This method returns the value of BigDecimal Object, rounded as necessary. A zero result will have a scale of 0.
    Below program illustrates the working of the above mentioned method: 
    Program 1: 
     

Java




// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        BigDecimal b1 = new BigDecimal("-452.325");
 
        MathContext m = new MathContext(4); // 4 precision
 
        // Perform plus on BigDecimal Objects b1 using m
        BigDecimal b2 = b1.plus(m);
 
        // Print the value of b2
        System.out.println("Result of plus is " + b2);
    }
}


  1.  
Output: 

Result of plus is -452.3

 

  1. Program 2: 
     

Java




// Java program to demonstrate the
// plus() method
 
import java.math.*;
 
public class gfg {
 
    public static void main(String[] args)
    {
 
        BigDecimal b1 = new BigDecimal("-10.325");
 
        // 4 precision
        MathContext m = new MathContext(4);
 
        // Perform plus on BigDecimal Objects b1 using m
        BigDecimal b2 = b1.plus(m);
 
        // Print the value of b2
        System.out.println("Result of plus is " + b2);
    }
}


  1.  
Output: 

Result of plus is -10.33

 

  1.  

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#plus()
 

RELATED ARTICLES

Most Popular

Recent Comments