getStandardUPCEANChecksum static method

int getStandardUPCEANChecksum(
  1. String s
)

Implementation

static int getStandardUPCEANChecksum(String s) {
  final length = s.length;
  int sum = 0;
  for (int i = length - 1; i >= 0; i -= 2) {
    final digit = s.codeUnitAt(i) - 48 /* 0 */;
    if (digit < 0 || digit > 9) {
      throw FormatsException.instance;
    }
    sum += digit;
  }
  sum *= 3;
  for (int i = length - 2; i >= 0; i -= 2) {
    final digit = s.codeUnitAt(i) - 48 /* 0 */;
    if (digit < 0 || digit > 9) {
      throw FormatsException.instance;
    }
    sum += digit;
  }
  return (1000 - sum) % 10;
}