How to round double numbers with limit the numbers with decimal places in Dart/ Flutter By Examples

This tutorial shows you how to round double numbers with a limit to the precision.

For example, if the double number is 12.12345, limit the number of decimal places from 3 to 12.123. Let’s see an example.

Double-precision after decimal places in dart

There are two ways we can convert double to limit decimal placesto n times. One way, use the double toStringAsFixed method.

The toStringAsFixed method takes fractionDigits and returns the string representation of double numbers with decimal digits limit fractionDigits. Syntax:

String toStringAsFixed(int fractionDigits)

The returns String version of a double value.

Convert string into double using double.parse() method.

Another way, use the double toStringAsExponential method.

The toStringAsExponential method takes fractionDigits and returns a string version of a double value with limited decimal places. Syntax:

String toStringAsExponential([int? fractionDigits])

Return String, which can be converted to double.parse() method

Here is an example

void main() {
  double d = 12.12349;
  String result = d.toStringAsFixed(4);
  print(result);

  var result1 = double.parse(d.toStringAsExponential(3));
  print(result1);
}

Output:

12.1235
12.123

Conclusion

To summarize, Limit the double floating decimal precision with examples.