Dart .toStringAsPrecision()
Anonymous contributor
Published Jun 27, 2024
Contribute to Docs
In Dart, .toStringAsPrecision() is a method used to convert a number to a double. It returns a string representation of that value with a specified precision of significant digits.
Syntax
number.toStringAsPrecision(int precision)
number: A double-precision floating-point number to be converted to a string representation.precision: Specifies the number of significant digits in the resulting string representation of the double-precision floating-point number. It must be an integer between 1 and 21, inclusive.
Example
The following example illustrates how the .toStringAsPrecision() method is utilized to convert this number to a double and returns a string representation of that value:
void main() {String a1 = 1.toStringAsPrecision(2);print("Output = " + a1);String a2 = 1e15.toStringAsPrecision(3);print("Output = " + a2);String a3 = 1234567.toStringAsPrecision(9);print("Output = " + a3);}
The above code produces the following output:
Output = 1.0Output = 1.00e+15Output = 1234567.00
Note:: To determine the type of the output at runtime, use the
.runtimeTypeproperty.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Dart on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours