{

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 precision.

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

Double precision after decimal places in dart

There are two ways we can convert double to limit decimal places to n times. One way, use the double toStringAsFixed method. toStringAsFixed method takes fractionDigits and returns the string representation of double number with decimal digits limit fractionDigits. Syntax:

String toStringAsFixed(int fractionDigits)

Returns String version double value.

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

Another way, use the double toStringAsExponential method.

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

String toStringAsExponential([int? fractionDigits])

Return String, that 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 precission with examples.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.