.toStringAsFixed()
Anonymous contributor
Anonymous contributor2 total contributions
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 anint
or adouble
data 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 contributorAnonymous contributor2 total contributions
- Anonymous contributor
Looking to contribute?
- 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.