{

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


 round double numbers with limit the numbers with decimal places in Dart

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.

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

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not