BigDecimal abs() Method in Java

04 Dec, 2018
The java.math.BigDecimal.abs() is used to return a BigDecimal whose value is the absolute value of the BigDecimal and whose scale is this.scale().Syntax :public BigDecimal abs()Parameters: The method does not accept any parameters.Return Value: Returns a BigDecimal whose value is the absolute value of this BigDecimal scale is this.scale().Below programs will illustrate the use of java.math.BigDecimal.abs() method :Program 1// Java program to demonstrate abs() methodimport java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Creating a BigDecimal object        BigDecimal num;          // Assigning value to num        num = new BigDecimal("-51");          // Displaying the result        System.out.println("Absolute value is " + num.abs());    }}Output:Absolute value is 51
Program 2// Java program to demonstrate abs() methodimport java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // creating a BigDecimal object        BigDecimal num;          // assign value to num        num = new BigDecimal("-63.93471");          System.out.println("Absolute value is " + num.abs());    }}Output:Absolute value is 63.93471
The java.math.BigDecimal.abs(MathContext mc) returns a BigDecimal whose value is the absolute value of the BigDecimal obtained by rounding it off according to the precision settings specified by mc, an object of MathContext class.Syntax:public BigDecimal abs(MathContext mc)Parameters : The function accepts only one parameter mc of MathContext class object, which specifies precision settings to be used for rounding off the BigDecimal.Return Value: Returns a BigDecimal whose value is the absolute value of this BigDecimal obtained by rounding it off according to the precision settings specified by the object mc.Exception : The method throws an ArithmeticException, if the result is inexact but the rounding mode is UNNECESSARY.Below programs illustrate the use of java.math.BigDecimal.abs() method with specified MathContext :Program 1import java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Create 2 BigDecimal objects        BigDecimal num, absv;          MathContext mc = new MathContext(2);          // Assign value to num        num = new BigDecimal("51.93471");          // Assign absolute value of num to absv rounded         // to 2 precision using mc        absv = num.abs(mc);          System.out.println("Absolute value, rounded to 2 precision is "        + absv);    }}Output:Absolute value, rounded to 2 precision is 52
Program 2import java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Create 2 BigDecimal objects        BigDecimal num, absv;          MathContext mc = new MathContext(15);          // Assign value to num        num = new BigDecimal("143567812363.93471");          // Assign absolute value of num to absv rounded         // to 15 precision using mc        absv = num.abs(mc);          System.out.println("Absolute value, rounded to 15 precision is "         + absv);    }}Output:Absolute value, rounded to 15 precision is 143567812363.935

The java.math.BigDecimal.abs() is used to return a BigDecimal whose value is the absolute value of the BigDecimal and whose scale is this.scale().Syntax :public BigDecimal abs()Parameters: The method does not accept any parameters.Return Value: Returns a BigDecimal whose value is the absolute value of this BigDecimal scale is this.scale().Below programs will illustrate the use of java.math.BigDecimal.abs() method :Program 1// Java program to demonstrate abs() methodimport java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Creating a BigDecimal object        BigDecimal num;          // Assigning value to num        num = new BigDecimal("-51");          // Displaying the result        System.out.println("Absolute value is " + num.abs());    }}Output:Absolute value is 51
Program 2// Java program to demonstrate abs() methodimport java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // creating a BigDecimal object        BigDecimal num;          // assign value to num        num = new BigDecimal("-63.93471");          System.out.println("Absolute value is " + num.abs());    }}Output:Absolute value is 63.93471

Syntax :
public BigDecimal abs()
Parameters: The method does not accept any parameters.
Return Value: Returns a BigDecimal whose value is the absolute value of this BigDecimal scale is this.scale().
Below programs will illustrate the use of java.math.BigDecimal.abs() method :Program 1
// Java program to demonstrate abs() methodimport java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Creating a BigDecimal object        BigDecimal num;          // Assigning value to num        num = new BigDecimal("-51");          // Displaying the result        System.out.println("Absolute value is " + num.abs());    }}
Absolute value is 51

Program 2
// Java program to demonstrate abs() methodimport java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // creating a BigDecimal object        BigDecimal num;          // assign value to num        num = new BigDecimal("-63.93471");          System.out.println("Absolute value is " + num.abs());    }}
Absolute value is 63.93471

The java.math.BigDecimal.abs(MathContext mc) returns a BigDecimal whose value is the absolute value of the BigDecimal obtained by rounding it off according to the precision settings specified by mc, an object of MathContext class.Syntax:public BigDecimal abs(MathContext mc)Parameters : The function accepts only one parameter mc of MathContext class object, which specifies precision settings to be used for rounding off the BigDecimal.Return Value: Returns a BigDecimal whose value is the absolute value of this BigDecimal obtained by rounding it off according to the precision settings specified by the object mc.Exception : The method throws an ArithmeticException, if the result is inexact but the rounding mode is UNNECESSARY.Below programs illustrate the use of java.math.BigDecimal.abs() method with specified MathContext :Program 1import java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Create 2 BigDecimal objects        BigDecimal num, absv;          MathContext mc = new MathContext(2);          // Assign value to num        num = new BigDecimal("51.93471");          // Assign absolute value of num to absv rounded         // to 2 precision using mc        absv = num.abs(mc);          System.out.println("Absolute value, rounded to 2 precision is "        + absv);    }}Output:Absolute value, rounded to 2 precision is 52
Program 2import java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Create 2 BigDecimal objects        BigDecimal num, absv;          MathContext mc = new MathContext(15);          // Assign value to num        num = new BigDecimal("143567812363.93471");          // Assign absolute value of num to absv rounded         // to 15 precision using mc        absv = num.abs(mc);          System.out.println("Absolute value, rounded to 15 precision is "         + absv);    }}Output:Absolute value, rounded to 15 precision is 143567812363.935

Syntax:
public BigDecimal abs(MathContext mc)
Parameters : The function accepts only one parameter mc of MathContext class object, which specifies precision settings to be used for rounding off the BigDecimal.
Return Value: Returns a BigDecimal whose value is the absolute value of this BigDecimal obtained by rounding it off according to the precision settings specified by the object mc.
Exception : The method throws an ArithmeticException, if the result is inexact but the rounding mode is UNNECESSARY.
Below programs illustrate the use of java.math.BigDecimal.abs() method with specified MathContext :Program 1
import java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Create 2 BigDecimal objects        BigDecimal num, absv;          MathContext mc = new MathContext(2);          // Assign value to num        num = new BigDecimal("51.93471");          // Assign absolute value of num to absv rounded         // to 2 precision using mc        absv = num.abs(mc);          System.out.println("Absolute value, rounded to 2 precision is "        + absv);    }}
Absolute value, rounded to 2 precision is 52

Program 2
import java.io.*;import java.math.*;  public class GFG {      public static void main(String[] args)    {          // Create 2 BigDecimal objects        BigDecimal num, absv;          MathContext mc = new MathContext(15);          // Assign value to num        num = new BigDecimal("143567812363.93471");          // Assign absolute value of num to absv rounded         // to 15 precision using mc        absv = num.abs(mc);          System.out.println("Absolute value, rounded to 15 precision is "         + absv);    }}
Absolute value, rounded to 15 precision is 143567812363.935

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#abs()
Java-BigDecimal
Java-Functions
Java-lang package
java-math
Java-math-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Multidimensional Arrays in Java
Stream In Java
Set in Java
Singleton Class in Java