digits method

Iterable<int> digits([
  1. int base = 10
])

Returns an iterable over the digits of this int in the given base.

The digits are produced from least significant to most significant. For example, 123.digits() yields 3, then 2, then 1.

For negative numbers, the digits of the absolute value are returned. For example, -123.digits() also yields 3, then 2, then 1.

The base must be greater than or equal to 2.

Implementation

Iterable<int> digits([int base = 10]) sync* {
  if (base < 2) throw RangeError.range(base, 2, null, 'base');
  if (this == 0) {
    yield 0;
  } else {
    var number = abs();
    while (number > 0) {
      yield number % base;
      number ~/= base;
    }
  }
}