Dart BigInt abs()
Syntax & Examples

BigInt.abs() method

The `abs()` method returns the absolute value of this integer.


Syntax of BigInt.abs()

The syntax of BigInt.abs() method is:

 BigInt abs() 

This abs() method of BigInt returns the absolute value of this integer.

Return Type

BigInt.abs() returns value of type BigInt.



✐ Examples

1 Calculate absolute value of an integer

In this example,

  1. We declare an integer number with a value of -5.
  2. We use the abs() method to calculate its absolute value.
  3. We print the absolute value to standard output.

Dart Program

void main() {
  int number = -5;
  BigInt absValue = BigInt.from(number).abs();
  print('Absolute value of -5: $absValue');
}

Output

Absolute value of -5: 5

2 Calculate absolute value of a double

In this example,

  1. We declare a double number with a value of -7.5.
  2. We convert it to an integer and then use the abs() method to calculate its absolute value as BigInt.
  3. We print the absolute value to standard output.

Dart Program

void main() {
  double number = -7.5;
  BigInt absValue = BigInt.from(number.toInt()).abs();
  print('Absolute value of -7.5: $absValue');
}

Output

Absolute value of -7.5: 7

3 Calculate absolute value of a BigInt

In this example,

  1. We create a BigInt bigInt with a large negative value.
  2. We use the abs() method to calculate its absolute value.
  3. We print the absolute value to standard output.

Dart Program

void main() {
  BigInt bigInt = BigInt.parse('-12345678901234567890');
  BigInt absValue = bigInt.abs();
  print('Absolute value of BigInt: $absValue');
}

Output

Absolute value of BigInt: 12345678901234567890

Summary

In this Dart tutorial, we learned about abs() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.