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,

  1. We create an integer variable num with the value 4.
  2. Since integers are whole numbers, calling the round() method on num returns the integer itself.
  3. 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,

  1. We create an integer variable negativeNum with the value -3.
  2. Since integers are whole numbers, calling the round() method on negativeNum returns the integer itself.
  3. 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,

  1. We create an integer variable integerNum with the value 7.
  2. Since integers are whole numbers, calling the round() method on integerNum returns the integer itself.
  3. 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.