How check digits work: Luhn, IBAN and ISBN explained
Most long numbers you type, such as a payment card, a bank account or a book code, end in digits that are not random. They are check digits, calculated from the rest of the number so that a typing mistake is caught straight away.
What a check digit does
A check digit is a small piece of arithmetic built into the number. When you type the number, the form repeats the calculation and compares. If one digit is wrong, or two neighbouring digits are swapped, the answer no longer matches and the form can warn you before anything is sent.
Check digits catch accidents. They do not prove that a number is real, that an account exists or that a card has money on it.
The Luhn algorithm (payment cards and more)
Luhn is used for payment card numbers and many other identifiers. Working from the right-hand end, double every second digit. If doubling gives a number above 9, subtract 9. Add everything up. If the total is a multiple of 10, the number passes.
Here is the standard textbook example, 79927398713:
digits from the right: 3 1 7 8 9 3 7 2 9 9 7
double every second: 3 2 7 7 9 6 7 4 9 9 7
(16 becomes 7, 18 becomes 9)
sum = 3+2+7+7+9+6+7+4+9+9+7 = 70 -> multiple of 10, validWhat Luhn can and cannot catch
- It catches every single wrong digit.
- It catches almost all swaps of two neighbouring digits. The one exception is swapping 0 and 9.
- It does not catch every error, and it offers no protection against anyone deliberately making up a number that passes.
IBAN: country, check digits and account
An IBAN starts with a two-letter country code and two check digits, followed by the national account number. The length depends on the country: Norway uses 15 characters, Germany 22 and Malta 31.
To check one, move the first four characters to the end, replace each letter with a number (A is 10, B is 11 and so on up to Z, which is 35), and divide the huge number by 97. A valid IBAN leaves a remainder of 1.
The UK example GB82 WEST 1234 5698 7654 32 passes. Change any single character and the remainder is no longer 1.
ISBN: weights and a modulus
ISBN-13, the current book number, multiplies the first twelve digits alternately by 1 and 3, adds them up, and chooses the last digit that brings the total to a multiple of 10. For 978-0-306-40615-7 the twelve digits add up to 93, so the check digit is 7.
The older ISBN-10 multiplies the nine digits by 10, 9, 8 and so on down to 2, adds them, and chooses the last digit that makes the total a multiple of 11. When that digit has to be 10, it is written as the letter X. For 0-306-40615-2 the sum is 130, and 2 is the digit that reaches 132, which is 12 times 11.
Converting between the two formats means putting 978 in front of the nine core digits and working out a new check digit. Numbers beginning 979 have no ISBN-10 form.
Barcodes use the same idea
The numbers under product barcodes, such as EAN-13, also end in a check digit calculated with alternating weights of 1 and 3, just like ISBN-13. Scanners use it to reject a misread.
Check a number yourself
Use the Luhn validator for card-style numbers, the IBAN validator for bank accounts and the ISBN tool for books. Each also tells you what the last digit should have been. Everything runs in your browser, but for your own safety use documented test numbers rather than a real card number when you experiment.