Skip to content

Instantly share code, notes, and snippets.

@vivahiraj
Created August 9, 2015 12:41
Show Gist options
  • Save vivahiraj/d9c0909a73538a3bb8f9 to your computer and use it in GitHub Desktop.
Save vivahiraj/d9c0909a73538a3bb8f9 to your computer and use it in GitHub Desktop.
モジュラス10ウェイト3でチェックデジットを計算する
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