Dart int round()
Syntax & Examples
int.round() method
The `round` method in Dart returns the integer itself because integers are already whole numbers and cannot be rounded to another integer.
Syntax of int.round()
The syntax of int.round() method is:
int round()
This round() method of int returns this.
Return Type
int.round() returns value of type int
.
✐ Examples
1 Round of an integer
In this example,
- We create an integer variable
num
with the value 4. - Since integers are whole numbers, calling the
round()
method onnum
returns the integer itself. - We then print the result to standard output.
Dart Program
void main() {
int num = 4;
int roundNum = num.round();
print('Round of $num: $roundNum');
}
Output
Round of 4: 4
2 Round of a negative integer
In this example,
- We create an integer variable
negativeNum
with the value -3. - Since integers are whole numbers, calling the
round()
method onnegativeNum
returns the integer itself. - We then print the result to standard output.
Dart Program
void main() {
int negativeNum = -3;
int roundNegative = negativeNum.round();
print('Round of $negativeNum: $roundNegative');
}
Output
Round of -3: -3
3 Round of an integer
In this example,
- We create an integer variable
integerNum
with the value 7. - Since integers are whole numbers, calling the
round()
method onintegerNum
returns the integer itself. - We then print the result to standard output.
Dart Program
void main() {
int integerNum = 7;
int roundInteger = integerNum.round();
print('Round of $integerNum: $roundInteger');
}
Output
Round of 7: 7
Summary
In this Dart tutorial, we learned about round() method of int: the syntax and few working examples with output and detailed explanation for each example.