Dart int abs()
Syntax & Examples
int.abs() method
The `abs` method in Dart returns the absolute value of this integer.
Syntax of int.abs()
The syntax of int.abs() method is:
int abs()
This abs() method of int returns the absolute value of this integer.
Return Type
int.abs() returns value of type int
.
✐ Examples
1 Absolute value of a negative number
In this example,
- We assign the value
-5
to the integer variablenum1
. - We calculate the absolute value of
num1
using theabs()
method. - We print the result to standard output.
Dart Program
void main() {
int num1 = -5;
int absNum1 = num1.abs();
print('Absolute value of $num1: $absNum1');
}
Output
Absolute value of -5: 5
2 Absolute value of a positive number
In this example,
- We assign the value
10
to the integer variablenum2
. - We calculate the absolute value of
num2
using theabs()
method. - We print the result to standard output.
Dart Program
void main() {
int num2 = 10;
int absNum2 = num2.abs();
print('Absolute value of $num2: $absNum2');
}
Output
Absolute value of 10: 10
3 Absolute value of zero
In this example,
- We assign the value
0
to the integer variablenum3
. - We calculate the absolute value of
num3
using theabs()
method. - We print the result to standard output.
Dart Program
void main() {
int num3 = 0;
int absNum3 = num3.abs();
print('Absolute value of $num3: $absNum3');
}
Output
Absolute value of 0: 0
Summary
In this Dart tutorial, we learned about abs() method of int: the syntax and few working examples with output and detailed explanation for each example.