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

Is there any way to convert from double to int in dart

I want to solve leetcode 172nd question using dart. Take the factorial of a given number and find how many zeros there are at the end.

I done until now

void main() {
  print(factorial(5));

}

factorial(int input) {
  int factorial = 1;
  var answer = 0;
  for (int i = 1; i <= input; i++) {
    factorial *= i;
  }

  while (factorial > 10 && factorial.toString().split('').last == "0") {
    factorial = (factorial / 10)

    answer++;
  }

  return answer;
}

but when i divide factorial by 10 it not allowed. and if assing at the begining like

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

double factorial=1;

this time the number is 120.0 and then zero is more. Can anyone help on this, thanks

>Solution :

You can use method for double to int;

double a = 8.5;
print(a.toInt())  // 8

Answer:

 while (factorial > 10 && factorial.toString().split('').last == "0") {
    factorial = (factorial / 10).toInt(); // Add toInt()

    answer++;
  }
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