- Java.math package classes
- Java.math - Home
- Java.math - BigDecimal
- Java.math - BigInteger
- Java.math - MathContext
- Java.math package extras
- Java.math - Enumerations
- Java.math - Discussion
Java.math.BigDecimal.round() Method
Description
The java.math.BigDecimal.round(MathContext mc) returns a BigDecimal rounded according to the MathContext settings. If the precision setting is 0 then no rounding takes place.The effect of this method is identical to that of the plus(MathContext) method.
Declaration
Following is the declaration for java.math.BigDecimal.round() method.
public BigDecimal round(MathContext mc)
Parameters
mc − The context to use.
Return Value
This method returns a BigDecimal rounded according to the MathContext settings.
Exception
ArithmeticException − If the rounding mode is UNNECESSARY and the BigDecimal operation would require rounding.
Example
The following example shows the usage of math.BigDecimal.round() 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("5.46497"); MathContext mc = new MathContext(3); // 3 precision // bg1 is rounded using mc bg2 = bg1.round(mc); String str = "The value " + bg1 + " after rounding is " + bg2; // print bg2 value System.out.println( str ); } }
Let us compile and run the above program, this will produce the following result −
The value 5.46497 after rounding is 5.46
java_math_bigdecimal.htm
Advertisements