Dart int toInt()
Syntax & Examples
int.toInt() method
The `toInt` method in Dart truncates this number to an integer and returns the result as an integer.
Syntax of int.toInt()
The syntax of int.toInt() method is:
int toInt()
This toInt() method of int truncates this num to an integer and returns the result as an int.
Return Type
int.toInt() returns value of type int
.
✐ Examples
1 Convert to integer
In this example,
- We create an int variable
num1
with the value 10. - We use the
toInt()
method to convert it to an integer. - We then print the result to standard output.
Dart Program
void main() {
int num1 = 10;
int result1 = num1.toInt();
print('Integer value of $num1: $result1');
}
Output
Integer value of 10: 10
2 Convert to integer
In this example,
- We create an int variable
num2
with the value 0. - We use the
toInt()
method to convert it to an integer. - We then print the result to standard output.
Dart Program
void main() {
int num2 = 0;
int result2 = num2.toInt();
print('Integer value of $num2: $result2');
}
Output
Integer value of 0: 0
3 Convert to integer
In this example,
- We create an int variable
num3
with the value -7. - We use the
toInt()
method to convert it to an integer. - We then print the result to standard output.
Dart Program
void main() {
int num3 = -7;
int result3 = num3.toInt();
print('Integer value of $num3: $result3');
}
Output
Integer value of -7: -7
Summary
In this Dart tutorial, we learned about toInt() method of int: the syntax and few working examples with output and detailed explanation for each example.