Java.math.BigDecimal.plus() Method

Advertisements


Description

The java.math.BigDecimal.plus(MathContext mc) returns a BigDecimal whose value is (+this), with rounding according to the context settings.

The effect of this method is identical to that of the round(MathContext) method.

Declaration

Following is the declaration for java.math.BigDecimal.plus() method

public BigDecimal plus(MathContext mc)

Parameters

  • mc - the context to use

Return Value

This method returns the value of BigDecimal Object, rounded as necessary. A zero result will have a scale of 0.

Exception

  • ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY

Example

The following example shows the usage of math.BigDecimal.plus() method

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

     public static void main(String[] args) {

        // create 2 BigDecimal Objects
        BigDecimal bg1, bg2;
        bg1 = new BigDecimal("-333.3454");

        MathContext mc = new MathContext(4); // 4 precision

        // perform plus on bg1 using mc
        bg2 = bg1.plus(mc);

	String str = "The Result of plus using context settings is " + bg2;

        // print the value of bg2
        System.out.println( str );
    }
}

Let us compile and run the above program, this will produce the following result:

The Result of plus using context settings is -333.3



Advertisements
Advertisements