Created
August 9, 2015 12:41
-
-
Save vivahiraj/d9c0909a73538a3bb8f9 to your computer and use it in GitHub Desktop.
モジュラス10ウェイト3でチェックデジットを計算する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calc_modulus10(v) | |
val = v.reverse | |
pos = total_even = total_odd = 0 | |
val.split(//).each do |d| | |
pos = pos + 1 | |
(pos % 2) == 0 ? total_even += d.to_i : total_odd += d.to_i | |
end | |
digit = 10 - (total_even + total_odd * 3).to_s[-1,1].to_i | |
digit = 0 if digit == 10 | |
return digit.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment