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








Syntax

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

public BigDecimal abs(MathContext mc)

Example

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

/*from  w w w .  j a  v  a 2 s .  c o  m*/

import java.math.BigDecimal;
import java.math.MathContext;

public class Main {

  public static void main(String[] args) {

    MathContext mc = new MathContext(2);
    MathContext mc1 = new MathContext(4);

    BigDecimal bg1 = new BigDecimal("123.1234");

    // assign absolute value of bg1 to bg2 rounded to 2 precision using mc
    BigDecimal bg2 = bg1.abs(mc);

    // assign absolute value of bg1 to bg3 rounded to 4 precision using mc1
    BigDecimal bg3 = bg1.abs(mc1);

    System.out.println(bg2);
    System.out.println(bg3);
  }
}

The code above generates the following result.