BigDecimal plus() method in Java
- 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()
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 metioned method:
Program 1:// Java program to demonstrate the// plus() methodimportjava.math.*;publicclassGfg {publicstaticvoidmain(String[] args){// Assign value to b1BigDecimal b1 =newBigDecimal("-45.652");// Assign the result of plus method on// BigDecimal Objects b1 to b2BigDecimal b2 = b1.plus();// Print the value of b2System.out.println("The value of the BigDecimal is "+ b2);}}chevron_rightfilter_noneOutput:The value of the BigDecimal is -45.652
Program 2:
// Java program to demonstrate the// plus() methodimportjava.math.*;publicclassgfg {publicstaticvoidmain(String[] args){// Assign value to b1BigDecimal b1 =newBigDecimal("7458.3256");// Assign the result of plus method on// BigDecimal Objects b1 to b2BigDecimal b2 = b1.plus();// Print the value of b2System.out.println("The value of the BigDecimal is "+ b2);}}chevron_rightfilter_noneOutput:The value of the BigDecimal is 7458.3256
- 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)
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 metioned method:
Program 1:// Java program to demonstrate the// plus() methodimportjava.math.*;publicclassgfg {publicstaticvoidmain(String[] args){BigDecimal b1 =newBigDecimal("-452.325");MathContext m =newMathContext(4);// 4 precision// Perform plus on BigDecimal Objects b1 using mBigDecimal b2 = b1.plus(m);// Print the value of b2System.out.println("Result of plus is "+ b2);}}chevron_rightfilter_noneOutput:Result of plus is -452.3
Program 2:
// Java program to demonstrate the// plus() methodimportjava.math.*;publicclassgfg {publicstaticvoidmain(String[] args){BigDecimal b1 =newBigDecimal("-10.325");// 4 precisionMathContext m =newMathContext(4);// Perform plus on BigDecimal Objects b1 using mBigDecimal b2 = b1.plus(m);// Print the value of b2System.out.println("Result of plus is "+ b2);}}chevron_rightfilter_noneOutput:Result of plus is -10.33
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#plus()
Recommended Posts:
- BigDecimal max() Method in Java
- BigDecimal min() Method in Java
- BigDecimal ulp() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal equals() Method in Java
- BigDecimal movePointLeft() Method in Java
- BigDecimal shortValueExact() Method in Java
- BigDecimal multiply() Method in Java
- BigDecimal scale() Method in Java
- BigDecimal signum() Method in Java
- BigDecimal pow() method in Java with Examples
- BigDecimal add() Method in Java with Examples
- BigDecimal movePointRight() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal stripTrailingZeros() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Shivam_k


