Dart int toStringAsPrecision()
Syntax & Examples
int.toStringAsPrecision() method
The `toStringAsPrecision` method in Dart converts this number to a double and returns a string representation with exactly the specified number of significant digits.
Syntax of int.toStringAsPrecision()
The syntax of int.toStringAsPrecision() method is:
String toStringAsPrecision(int precision) This toStringAsPrecision() method of int converts this to a double and returns a string representation with exactly precision significant digits.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
precision | required | The number of significant digits. |
Return Type
int.toStringAsPrecision() returns value of type String.
✐ Examples
1 String representation with precision 3
In this example,
- We assign the value
12345to the integer variablenum. - We convert
numto a string representation with precision 3 using thetoStringAsPrecision()method. - We print the result to standard output.
Dart Program
void main() {
int num = 12345;
String precisionString = num.toStringAsPrecision(3);
print('String representation of $num with precision 3: $precisionString');
}Output
String representation of 12345 with precision 3: 1.23e+4
2 String representation with precision 5
In this example,
- We assign the value
987654to the integer variablenum. - We convert
numto a string representation with precision 5 using thetoStringAsPrecision()method. - We print the result to standard output.
Dart Program
void main() {
int num = 987654;
String precisionString = num.toStringAsPrecision(5);
print('String representation of $num with precision 5: $precisionString');
}Output
String representation of 987654 with precision 5: 98765
3 String representation with precision 7
In this example,
- We assign the value
123to the integer variablenum. - We convert
numto a string representation with precision 7 using thetoStringAsPrecision()method. - We print the result to standard output.
Dart Program
void main() {
int num = 123;
String precisionString = num.toStringAsPrecision(7);
print('String representation of $num with precision 7: $precisionString');
}Output
String representation of 123 with precision 7: 123.0000
Summary
In this Dart tutorial, we learned about toStringAsPrecision() method of int: the syntax and few working examples with output and detailed explanation for each example.