computeCheckDigit static method

int computeCheckDigit(
  1. String value
)

Compute the check digit of the sequence of numbers value and return it as an integer

Please not that for now, we do not check that the input sequence is actually made of only digits

Implementation

static int computeCheckDigit(String value) {
  final runes = value.runes;
  var sum = 0;
  for (var i = runes.length - 1, j = 0; i >= 0; --i, j++) {
    var digit = runes.elementAt(i) - ZERO;
    if (j % 2 == 0) {
      digit *= 2;
      if (digit > 9) {
        digit -= 9;
      }
    }
    sum += digit;
  }
  if (sum % 10 == 0) {
    return 0;
  }
  return 10 - sum % 10;
}