Yunus Kocatas
Yunus Kocatas

Reputation: 316

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

double factorial=1;

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

Upvotes: 0

Views: 344

Answers (3)

Ber
Ber

Reputation: 41793

Check the accepted answer to this Question: How to do Integer division in Dart?

Integer division in Dart has its own operator: ~/ as in

print(16 ~/ 3);

Upvotes: 1

Yasin Ege
Yasin Ege

Reputation: 723

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

Upvotes: 3

Md. Kamrul Amin
Md. Kamrul Amin

Reputation: 2415

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.

Upvotes: 1

Related Questions