Dart num toStringAsFixed()
Syntax & Examples
num.toStringAsFixed() method
The `toStringAsFixed` method in Dart returns a decimal-point string representation of this number.
Syntax of num.toStringAsFixed()
The syntax of num.toStringAsFixed() method is:
String toStringAsFixed(int fractionDigits)
This toStringAsFixed() method of num returns a decimal-point string-representation of this.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
fractionDigits | required | The number of digits to display after the decimal point. |
Return Type
num.toStringAsFixed() returns value of type String
.
✐ Examples
1 Formatting a number with 2 decimal places
In this example,
- We create a num variable
number
with the value 123.456789. - We use the
toStringAsFixed
method with argument 2 to format the number with 2 decimal places. - We then print the formatted number to standard output.
Dart Program
void main() {
num number = 123.456789;
String result = number.toStringAsFixed(2);
print('Formatted number: $result');
}
Output
Formatted number: 123.46
2 Formatting a number with 4 decimal places
In this example,
- We create a num variable
number
with the value 987.654321. - We use the
toStringAsFixed
method with argument 4 to format the number with 4 decimal places. - We then print the formatted number to standard output.
Dart Program
void main() {
num number = 987.654321;
String result = number.toStringAsFixed(4);
print('Formatted number: $result');
}
Output
Formatted number: 987.6543
Summary
In this Dart tutorial, we learned about toStringAsFixed() method of num: the syntax and few working examples with output and detailed explanation for each example.