Dart double roundToDouble()
Syntax & Examples
double.roundToDouble() method
The `roundToDouble` method in Dart returns the integer double value closest to the given number.
Syntax of double.roundToDouble()
The syntax of double.roundToDouble() method is:
double roundToDouble()
This roundToDouble() method of double returns the integer double value closest to this
.
Return Type
double.roundToDouble() returns value of type double
.
✐ Examples
1 Round a decimal number to the nearest integer
In this example,
- We create a double variable
num
with the value 4.2. - We use the
roundToDouble()
method to roundnum
to the nearest integer as a double value. - We then print the rounded value to standard output.
Dart Program
void main() {
double num = 4.2;
double rounded = num.roundToDouble();
print('Rounded value of $num: $rounded');
}
Output
Rounded value of 4.2: 4
2 Round a decimal number to the nearest integer (upwards)
In this example,
- We create a double variable
num
with the value 4.8. - We use the
roundToDouble()
method to roundnum
to the nearest integer as a double value. - We then print the rounded value to standard output.
Dart Program
void main() {
double num = 4.8;
double rounded = num.roundToDouble();
print('Rounded value of $num: $rounded');
}
Output
Rounded value of 4.8: 5
3 Round a negative decimal number to the nearest integer
In this example,
- We create a double variable
num
with the value -4.5. - We use the
roundToDouble()
method to roundnum
to the nearest integer as a double value. - We then print the rounded value to standard output.
Dart Program
void main() {
double num = -4.5;
double rounded = num.roundToDouble();
print('Rounded value of $num: $rounded');
}
Output
Rounded value of -4.5: -5
Summary
In this Dart tutorial, we learned about roundToDouble() method of double: the syntax and few working examples with output and detailed explanation for each example.