Dart int toDouble()
Syntax & Examples
int.toDouble() method
The `toDouble` method in Dart converts this number to a double.
Syntax of int.toDouble()
The syntax of int.toDouble() method is:
double toDouble()
This toDouble() method of int return this num as a double.
Return Type
int.toDouble() returns value of type double
.
✐ Examples
1 Convert a positive integer to double
In this example,
- We assign the value
5
to the integer variablenum
. - We convert
num
to a double using thetoDouble()
method. - We print the result to standard output.
Dart Program
void main() {
int num = 5;
double doubleNum = num.toDouble();
print('Double value of $num: $doubleNum');
}
Output
Double value of 5: 5
2 Convert a negative integer to double
In this example,
- We assign the value
-3
to the integer variablenum
. - We convert
num
to a double using thetoDouble()
method. - We print the result to standard output.
Dart Program
void main() {
int num = -3;
double doubleNum = num.toDouble();
print('Double value of $num: $doubleNum');
}
Output
Double value of -3: -3
3 Convert a positive integer to double
In this example,
- We assign the value
10
to the integer variablenum
. - We convert
num
to a double using thetoDouble()
method. - We print the result to standard output.
Dart Program
void main() {
int num = 10;
double doubleNum = num.toDouble();
print('Double value of $num: $doubleNum');
}
Output
Double value of 10: 10
Summary
In this Dart tutorial, we learned about toDouble() method of int: the syntax and few working examples with output and detailed explanation for each example.