2

Suppose a = 1, and we want to convert it to double Two Approaches I know:-

  1. double.parse(a.toString())
  2. a * 1.0

Which one is more efficient and why?

Also if anyone knows another approach let me know

Thanks!

EDIT

Alternative Approaches:

  1. a.toDouble() -Oshibka404
SDS712
  • 103
  • 1
  • 1
  • 7
  • Is this useful to your question? https://stackoverflow.com/questions/7453505/why-a-double-1-0-its-equal-to-an-int-1 – Ebenezer Isaac Jun 06 '20 at 08:24
  • The most canonical way is to use `a.toDouble()`. Conversion of an integer into String followed by parsing it into double seems sub-optimal almost by definition. I suppose, `a*1.0` would do nearly the same as `a.toDouble()` under the hood, but using `toDouble` looks semantically clearer. – Andrey Ozornin Jun 06 '20 at 08:57

1 Answers1

1

this has to do with Complexity.

double x = a * 1.0; this runs only once.

double x = double.parse(a.toString()); technically this runs 2 or more times. it converts to a string first and then convert it to a double. (pop into the double definitions in the flutter framework to learn more.)

so first method is simpler and efficient.

Srilal Sachintha
  • 1,335
  • 1
  • 12
  • 18