Created
June 1, 2011 18:43
-
-
Save singpolyma/1002978 to your computer and use it in GitHub Desktop.
Convert ISBN10 to ISBN13
This file contains hidden or 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 isbn10_to_isbn13(isbn10) | |
i = 30 | |
s = '978' + isbn10.to_s[0..8] | |
4.step(12, 2) do |j| | |
i += s[j-2, 1].to_i + (3 * s[j-1, 1].to_i) | |
end | |
s + ((10 - (i % 10)) % 10).to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment