What is the round() method in Dart?

Overview

To round a double value to an int value in Dart, the method round() is used.

The round() method returns the value of a number rounded to the nearest integer.

Syntax

num.round()

Parameter

The method round() takes no parameter.

Return type

This method returns the value of a number rounded to the nearest integer.

Code

The following code shows how to use the method round() in Dart:

void main() {
//converting to nearest int value
int num = (-2.3).round();
print('The temperature is ${num} in Texas');
int num2 = (-2.5).round();
print(num2);
double n1 = 12.023;
double n2 = 12.89;
// compares if values are equal
if (n1.round() == n2.round()){
print('There are 12 apples in the basket');
}
else {
print('Not equal');
}
// display result
var value = n1.round();
print( value );
value = n2.round();
print( value );
}