Dart double abs()
Syntax & Examples


double.abs() method

The `abs` method returns the absolute value of a double.


Syntax of double.abs()

The syntax of double.abs() method is:

 double abs() 

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

Return Type

double.abs() returns value of type double.



✐ Examples

1 Find the absolute value of a negative number

In this example,

  1. We define a double num with a value of -3.14.
  2. We call the abs() method on num to find its absolute value.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = -3.14;
  double absValue = num.abs();
  print('Absolute value of -3.14: $absValue');
}

Output

Absolute value of -3.14: 3.14

2 Find the absolute value of a positive number

In this example,

  1. We define a double num with a value of 5.0.
  2. We call the abs() method on num to find its absolute value.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = 5.0;
  double absValue = num.abs();
  print('Absolute value of 5.0: $absValue');
}

Output

Absolute value of 5.0: 5.0

3 Find the absolute value of a negative decimal

In this example,

  1. We define a double num with a value of -7.5.
  2. We call the abs() method on num to find its absolute value.
  3. We print the result to standard output.

Dart Program

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

Output

Absolute value of -7.5: 7.5

Summary

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