Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert double in Dart

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading