Method: Gtiny::CheckDigitCalculators.gs1
- Defined in:
- lib/gtiny/check_digit_calculators.rb
.gs1(digits) ⇒ String
Calculate the check digit for any GTIN or ISBN-13.
There’s a great visualization of this algorithm here: www.gs1.org/services/how-calculate-check-digit-manually
13 14 15 16 17 18 19 20 21 |
# File 'lib/gtiny/check_digit_calculators.rb', line 13 def self.gs1(digits) sum = digits.reverse.each_char.with_index.sum do |digit, idx| multiplier = (idx % 2).zero? ? 3 : 1 multiplier * digit.to_i end check_digit = (10 - sum) % 10 check_digit.to_s end |