Search code examples
performancefluttertype-conversionintdouble

Flutter: Which is more efficient for getting a double from int


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

Solution

  • 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.