Converts the given numeric value to a double and returns a string representation with the specified number of significant digits.
The syntax of double.toStringAsPrecision() method is:
String toStringAsPrecision(int precision)
This toStringAsPrecision() method of double 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 |
double.toStringAsPrecision() returns value of type String
.
In this example,
numValue
with the value 12345.6789.toStringAsPrecision()
method with 5 significant digits specified.void main() {
num numValue = 12345.6789;
String precisionStr = numValue.toStringAsPrecision(5);
print(precisionStr);
}
12346
In this example,
numValue
with the value 12345.6789.toStringAsPrecision()
method with 10 significant digits specified.void main() {
num numValue = 12345.6789;
String precisionStr = numValue.toStringAsPrecision(10);
print(precisionStr);
}
12345.67890
In this example,
numValue
with the value 12345.6789.toStringAsPrecision()
method with 2 significant digits specified.void main() {
num numValue = 12345.6789;
String precisionStr = numValue.toStringAsPrecision(2);
print(precisionStr);
}
1.2e+4
In this Dart tutorial, we learned about toStringAsPrecision() method of double: the syntax and few working examples with output and detailed explanation for each example.