Dart num toString()
Syntax & Examples
num.toString() method
The `toString` method in Dart returns the shortest string that correctly represents the input number.
Syntax of num.toString()
The syntax of num.toString() method is:
String toString()
This toString() method of num returns the shortest string that correctly represent the input number.
Return Type
num.toString() returns value of type String
.
✐ Examples
1 String representation of an integer
In this example,
- We create a num variable
num1
with the value 4. - We use the
toString()
method to get its string representation. - We then print the result to standard output.
Dart Program
void main() {
num num1 = 4;
String stringValue = num1.toString();
print('String representation of number: $stringValue');
}
Output
String representation of number: 4
2 String representation of a decimal number
In this example,
- We create a num variable
num1
with the value 3.14. - We use the
toString()
method to get its string representation. - We then print the result to standard output.
Dart Program
void main() {
num num1 = 3.14;
String stringValue = num1.toString();
print('String representation of number: $stringValue');
}
Output
String representation of number: 3.14
3 String representation of a negative integer
In this example,
- We create a num variable
num1
with the value -10. - We use the
toString()
method to get its string representation. - We then print the result to standard output.
Dart Program
void main() {
num num1 = -10;
String stringValue = num1.toString();
print('String representation of number: $stringValue');
}
Output
String representation of number: -10
Summary
In this Dart tutorial, we learned about toString() method of num: the syntax and few working examples with output and detailed explanation for each example.