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