Home > database >  Is there any way to convert from double to int in dart
Is there any way to convert from double to int in dart

Time:02-03

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

double factorial=1;

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

CodePudding user response:

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

CodePudding user response:

To convert a double to int, just use:

double x = 1.256;
print(x.toInt()); //this will print 1

I am not sure what you are asking in this question other than this.

  •  Tags:  
  • Related