Dart num round()
Syntax & Examples


num.round() method

The `round` method in Dart returns the integer closest to this.


Syntax of num.round()

The syntax of num.round() method is:

 int round() 

This round() method of num returns the integer closest to this.

Return Type

num.round() returns value of type int.



✐ Examples

1 Rounding a decimal number

In this example,

  1. We create a num variable number1 with the value 5.6 and another variable number2 with the value -3.2.
  2. We use the round() method to get their rounded values as integers.
  3. We then print the results to standard output.

Dart Program

void main() {
  num number1 = 5.6;
  num number2 = -3.2;
  int result1 = number1.round();
  int result2 = number2.round();
  print('Rounded value of $number1: $result1');
  print('Rounded value of $number2: $result2');
}

Output

Rounded value of 5.6: 6
Rounded value of -3.2: -3

2 Rounding a decimal number with fractional part

In this example,

  1. We create a num variable number with the value 10.4.
  2. We use the round() method to get its rounded value as an integer.
  3. We then print the result to standard output.

Dart Program

void main() {
  num number = 10.4;
  int result = number.round();
  print('Rounded value of $number: $result');
}

Output

Rounded value of 10.4: 10

Summary

In this Dart tutorial, we learned about round() method of num: the syntax and few working examples with output and detailed explanation for each example.