Java Data Type Tutorial - Java BigDecimal.round(MathContext mc)








Syntax

BigDecimal.round(MathContext mc) has the following syntax.

public BigDecimal round(MathContext mc)

Example

In the following code shows how to use BigDecimal.round(MathContext mc) method.

import java.math.BigDecimal;
import java.math.MathContext;
/*from  ww w .java2  s. co m*/
public class Main {

  public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("5.46497");

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

    // bg1 is rounded using mc
    BigDecimal bg2 = bg1.round(mc);

    String str = "The value " + bg1 + " after rounding is " + bg2;

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

The code above generates the following result.