Created
October 13, 2017 12:27
-
-
Save juzow13/c0eea1b5fc253ad5a19ea9a3b9ba430b to your computer and use it in GitHub Desktop.
convert isbn13 to isbn10
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
# isbn is isbn13 | |
def to_isbn10(isbn) | |
digits = isbn.slice(3,9).split("") | |
sum = 0 | |
(0..9).each do |i| | |
sum += digits[i].to_i * (10 - i) | |
end | |
chk_tmp = 11 - (sum % 11) | |
chk_digit = "" | |
if chk_tmp == 10 | |
chk_digit = 'x' | |
elsif chk_tmp == 11 | |
chk_digit = 0 | |
else | |
chk_digit = chk_tmp | |
end | |
digits.push(chk_digit) | |
return digits.join("") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment