The `toStringAsPrecision` method in Dart converts this number to a double and returns a string representation with exactly the specified number of significant digits.
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.
Parameter | Optional/Required | Description |
---|---|---|
precision | required | The number of significant digits. |
int.toStringAsPrecision() returns value of type String
.
In this example,
12345
to the integer variable num
.num
to a string representation with precision 3 using the toStringAsPrecision()
method.void main() {
int num = 12345;
String precisionString = num.toStringAsPrecision(3);
print('String representation of $num with precision 3: $precisionString');
}
String representation of 12345 with precision 3: 1.23e+4
In this example,
987654
to the integer variable num
.num
to a string representation with precision 5 using the toStringAsPrecision()
method.void main() {
int num = 987654;
String precisionString = num.toStringAsPrecision(5);
print('String representation of $num with precision 5: $precisionString');
}
String representation of 987654 with precision 5: 98765
In this example,
123
to the integer variable num
.num
to a string representation with precision 7 using the toStringAsPrecision()
method.void main() {
int num = 123;
String precisionString = num.toStringAsPrecision(7);
print('String representation of $num with precision 7: $precisionString');
}
String representation of 123 with precision 7: 123.0000
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.