The `toStringAsFixed` method converts the given number to a string with a fixed number of decimal places.
The syntax of double.toStringAsFixed() method is:
String toStringAsFixed(int fractionDigits)
This toStringAsFixed() method of double returns a decimal-point string-representation of this
.
Parameter | Optional/Required | Description |
---|---|---|
fractionDigits | required | the number of digits after the decimal point |
double.toStringAsFixed() returns value of type String
.
In this example,
num
with a value of 3.14159
.toStringAsFixed
method on num
with 2
as the number of digits after the decimal point.void main() {
double num = 3.14159;
String result = num.toStringAsFixed(2);
print('Result: $result');
}
Result: 3.14
In this example,
num
with a value of 123.456789
.toStringAsFixed
method on num
with 3
as the number of digits after the decimal point.void main() {
double num = 123.456789;
String result = num.toStringAsFixed(3);
print('Result: $result');
}
Result: 123.457
In this example,
num
with a value of 987.654321
.toStringAsFixed
method on num
with 4
as the number of digits after the decimal point.void main() {
double num = 987.654321;
String result = num.toStringAsFixed(4);
print('Result: $result');
}
Result: 987.6543
In this Dart tutorial, we learned about toStringAsFixed() method of double: the syntax and few working examples with output and detailed explanation for each example.