{

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 places to 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)

Returns String version 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 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 precision 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.





Related posts

Dart| Flutter How to calculate the collection numbers sum example

Dart| Flutter: How to add logging to an application with configuration and examples

How to Sort List of objects - int string date properties in ascending and descending in Dart or Flutter example

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

Dart| Flutter: Difference between dynamic vs object with examples

How to convert Integer to String or String to Integer in Dart or Flutter | Dart By Example

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples