Skip to content

Instantly share code, notes, and snippets.

@saliouseck2009
Created February 11, 2022 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saliouseck2009/db1c639a8681deebf8f0554c2e63988d to your computer and use it in GitHub Desktop.
Save saliouseck2009/db1c639a8681deebf8f0554c2e63988d to your computer and use it in GitHub Desktop.
help you get closest string representation with exactly N digits after the decimal point, then parse the result to double
double num1 = double.parse((12.3412).toStringAsFixed(2));
// 12.34
double num2 = double.parse((12.5668).toStringAsFixed(2));
// 12.57
double num3 = double.parse((-12.3412).toStringAsFixed(2));
// -12.34
double num4 = double.parse((-12.3456).toStringAsFixed(2));
// -12.35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment