Dart num abs()
Syntax & Examples
num.abs() method
The `abs` method in Dart returns the absolute value of the given num.
Syntax of num.abs()
The syntax of num.abs() method is:
num abs()
This abs() method of num returns the absolute value of this num.
Return Type
num.abs() returns value of type num
.
✐ Examples
1 Absolute value of a negative decimal number
In this example,
- We create a num variable
negativeNum
with the value -3.8. - We use the
abs()
method to get its absolute value. - We then print the result to standard output.
Dart Program
void main() {
num negativeNum = -3.8;
num absValue = negativeNum.abs();
print('Absolute value of $negativeNum: $absValue');
}
Output
Absolute value of -3.8: 3.8
2 Absolute value of a negative integer
In this example,
- We create an num variable
positiveNum
with the value 7. - We use the
abs()
method to get its absolute value. - We then print the result to standard output.
Dart Program
void main() {
num positiveNum = 7;
num absValue = positiveNum.abs();
print('Absolute value of $positiveNum: $absValue');
}
Output
Absolute value of 7: 7
3 Absolute value of zero
In this example,
- We create a num variable
zero
with the value 0. - We use the
abs()
method to get its absolute value. - We then print the result to standard output.
Dart Program
void main() {
num zero = 0;
num absValue = zero.abs();
print('Absolute value of $zero: $absValue');
}
Output
Absolute value of 0: 0
Summary
In this Dart tutorial, we learned about abs() method of num: the syntax and few working examples with output and detailed explanation for each example.