'Convert double in Dart [duplicate]

I have the following number in decimal double x = 2.888888888888889; , but I have not been able to obtain 2.9 as a result.How is it done? is there a way to do this? that approaches 1 decimal more like the rules of mathematics? if it is 3.5 that the result is 3.6 ?



Solution 1:[1]

There are at least two ways, check this out:

void main() {

  double number = 2.888888888;
  
  double roundedByString = double.parse(number.toStringAsFixed(1));
  print(roundedByString); // prints 2.9
  
  double roundedByTenthInt = (number * 10).round() / 10;
  print(roundedByTenthInt); // prints 2.9
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tmaihoff