Dart .toStringAsFixed()
Anonymous contributor
Published May 23, 2024
Contribute to Docs
In Dart, .toStringAsFixed() is a method used to convert numeric data types, typically used with double, to a string representation with a fixed number of decimal places. If the input number is an integer, it will first be converted to a double before computing the string representation.
Syntax
numericValue.toStringAsFixed(fractionDigits);
numericValue: Represents the numeric value to convert to astring. It can be either anintor adoubledata type.fractionDigits: An integer indicating the decimal places in the resulting string. It must be non-negative and will pad with zeros if greater than the actual decimal places in the number.
Example
The following example illustrates how the .toStringAsFixed() method is utilized to convert a number into a string with the specified decimal digits:
void main() {double number = 3.14159;String stringNumber = number.toStringAsFixed(2);print("Output = ${stringNumber}");}
The above code produces the following output:
Output = 3.14
Note: The output 3.14 is a string. This can be confirmed by checking
value.runtimeType.
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