Dart double round()
Syntax & Examples


double.round() method

The `round` method returns the integer closest to the given double.


Syntax of double.round()

The syntax of double.round() method is:

 int round() 

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

Return Type

double.round() returns value of type int.



✐ Examples

1 Round a positive decimal

In this example,

  1. We define a double num with a value of 3.6.
  2. We call the round() method on num to round it to the nearest integer.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = 3.6;
  int roundedValue = num.round();
  print('Rounded value of 3.6: $roundedValue');
}

Output

Rounded value of 3.6: 4

2 Round a negative decimal

In this example,

  1. We define a double num with a value of -4.3.
  2. We call the round() method on num to round it to the nearest integer.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = -4.3;
  int roundedValue = num.round();
  print('Rounded value of -4.3: $roundedValue');
}

Output

Rounded value of -4.3: -4

3 Round a decimal with fractional part

In this example,

  1. We define a double num with a value of 7.8.
  2. We call the round() method on num to round it to the nearest integer.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = 7.8;
  int roundedValue = num.round();
  print('Rounded value of 7.8: $roundedValue');
}

Output

Rounded value of 7.8: 8

Summary

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