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,
- We define a double
numwith a value of3.6. - We call the
round()method onnumto round it to the nearest integer. - 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,
- We define a double
numwith a value of-4.3. - We call the
round()method onnumto round it to the nearest integer. - 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,
- We define a double
numwith a value of7.8. - We call the
round()method onnumto round it to the nearest integer. - 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.