Dart int toStringAsFixed()
Syntax & Examples


int.toStringAsFixed() method

The `toStringAsFixed` method in Dart returns a decimal-point string representation of this number with a specified number of fraction digits.


Syntax of int.toStringAsFixed()

The syntax of int.toStringAsFixed() method is:

 String toStringAsFixed(int fractionDigits) 

This toStringAsFixed() method of int returns a decimal-point string-representation of this.

Parameters

ParameterOptional/RequiredDescription
fractionDigitsrequiredThe number of digits after the decimal point.

Return Type

int.toStringAsFixed() returns value of type String.



✐ Examples

1 Decimal-point representation

In this example,

  1. We create an int variable num1 with the value 1000.
  2. We use the toStringAsFixed(2) method to get a fixed decimal representation with 2 fraction digits.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num1 = 1000;
  String result1 = num1.toStringAsFixed(2);
  print('Fixed decimal representation of $num1: $result1');
}

Output

Fixed decimal representation of 1000: 1000.00

2 Decimal-point representation

In this example,

  1. We create an int variable num2 with the value -25.
  2. We use the toStringAsFixed(3) method to get a fixed decimal representation with 3 fraction digits.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num2 = -25;
  String result2 = num2.toStringAsFixed(3);
  print('Fixed decimal representation of $num2: $result2');
}

Output

Fixed decimal representation of -25: -25.000

Summary

In this Dart tutorial, we learned about toStringAsFixed() method of int: the syntax and few working examples with output and detailed explanation for each example.