Java.math.BigInteger.abs() Method

Advertisements


Description

The java.math.BigInteger.abs() returns a BigInteger whose value is the absolute value of this BigInteger.

Declaration

Following is the declaration for java.math.BigInteger.abs() method

public BigInteger abs()

Parameters

  • NA

Return Value

This method returns the absolute value of the called value i.e abs(this).

Exception

  • NA

Example

The following example shows the usage of math.BigInteger.abs() method

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

	// create 4 BigInteger objects
	BigInteger bi1, bi2, bi3, bi4;  

	// assign values to bi1, bi2
	bi1 = new BigInteger("123");
	bi2 = new BigInteger("-123");

	// assign absolute values of bi1, bi2 to bi3, bi4
	bi3 = bi1.abs();
	bi4 = bi2.abs();
	  
	String str1 = "Absolute value of " + bi1 + " is " + bi3;
	String str2 = "Absolute value of " + bi2 + " is " + bi4;

	// print bi3, bi4 values
	System.out.println( str1 );
	System.out.println( str2 );
   }
}

Let us compile and run the above program, this will produce the following result:

Absolute value of 123 is 123
Absolute value of -123 is 123



Advertisements
Advertisements