Dart int toString()
Syntax & Examples
int.toString() method
The `toString` method in Dart returns a string representation of this integer.
Syntax of int.toString()
The syntax of int.toString() method is:
String toString()
This toString() method of int returns a string representation of this integer.
Return Type
int.toString() returns value of type String
.
✐ Examples
1 Convert to string
In this example,
- We create an integer variable
num
with the value 123. - We use the
toString()
method to convert it to a string. - We then print the result to standard output.
Dart Program
void main() {
int num = 123;
String numString = num.toString();
print('String representation of $num: $numString');
}
Output
String representation of 123: 123
2 Convert negative number to string
In this example,
- We create an integer variable
negativeNum
with the value -456. - We use the
toString()
method to convert it to a string. - We then print the result to standard output.
Dart Program
void main() {
int negativeNum = -456;
String negativeString = negativeNum.toString();
print('String representation of $negativeNum: $negativeString');
}
Output
String representation of -456: -456
3 Convert zero to string
In this example,
- We create an integer variable
zero
with the value 0. - We use the
toString()
method to convert it to a string. - We then print the result to standard output.
Dart Program
void main() {
int zero = 0;
String zeroString = zero.toString();
print('String representation of $zero: $zeroString');
}
Output
String representation of 0: 0
Summary
In this Dart tutorial, we learned about toString() method of int: the syntax and few working examples with output and detailed explanation for each example.