Friday, October 3, 2025
HomeLanguagesJavaBigDecimal round() Method in Java

BigDecimal round() Method in Java

The java.math.BigDecimal.round(MathContext m) is an inbuilt method in Java that returns a BigDecimal value rounded according to the MathContext settings. If the precision setting is 0 then no rounding takes place.

Syntax:

public BigDecimal round(MathContext m)

Parameters: The method accepts a single parameter m which refers to the context to use that is the value up to which the BigDecimal value is to be rounded off.

Return value: This method returns a BigDecimal rounded according to the MathContext settings.

Below programs illustrate the working of java.math.BigDecimal.round(MathContext m) method:
Program 1:




// Java program to demonstrate the
// round() method
import java.math.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
        // Assign value to BigDecimal object b1
        BigDecimal b1 = new BigDecimal("4.2585");
  
        MathContext m = new MathContext(4); // 4 precision
  
        // b1 is rounded using m
        BigDecimal b2 = b1.round(m);
  
        // Print b2 value
        System.out.println("The value of " + b1 + 
        " after rounding is " + b2);
    }
}


Output:

The value of 4.2585 after rounding is 4.259

Program 2:




// Java program to demonstrate the
// round() method
import java.math.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
        // Assigning value to BigDecimal object b1
        BigDecimal b1 = new BigDecimal("-4.2585");
  
        MathContext m = new MathContext(4); // 4 precision
  
        // b1 is rounded using m
        BigDecimal b2 = b1.round(m);
  
        // Print b2 value
        System.out.println("The value of " + b1 + 
        " after rounding is " + b2);
    }
}


Output:

The value of -4.2585 after rounding is -4.259

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS