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,
- We define a double
num
with a value of-3.14
. - We call the
abs()
method onnum
to find its absolute value. - 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,
- We define a double
num
with a value of5.0
. - We call the
abs()
method onnum
to find its absolute value. - 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,
- We define a double
num
with a value of-7.5
. - We call the
abs()
method onnum
to find its absolute value. - 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.