Last updated on February 21, 2023
The round() method converts the double value into an integer, and also it rounds up the value. It doesn’t take any parameter and returns the value with an integer data type.
Syntax
number.round()
Parameters
The round() method doesn’t accept any parameter.
Example 1
Create a variable and assign a float value.
var doubleValue = 20.40;
Next, attach the round() method to the variable through dot nation to convert it into an integer.
var convertToInteger = doubleValue.round();
print(convertToInteger);
// Output: 20
In this example, the round() method rounded the value and converted it into an integer. The converted value is 20, it is because the original value was 20.40 which should be decreased as per the rule. If the value had been 20.51 then the converted value would have been 21.
To check out the type of the variable you can access runtimeType through dot notation.
print(doubleValue.runtimeType);
// Output: double
Below are a few examples,
var doubleValue = 20.51;
var convertToInteger = doubleValue.round();
print(convertToInteger);
// Output: 21
var doubleValue = 30.90;
var convertToInteger = doubleValue.round();
print(convertToInteger);
// Output: 31