· Code types · 4 min read

How a check digit is calculated

Digits are weighted 1 and 3 alternately and summed; the amount needed to reach a multiple of ten is the check digit.

When a code is typed by hand it is easy to slip on one digit, and the software then opens a different product. The last digit of a barcode exists precisely to catch such mistakes. It is the simplest form of internal control: the error is found at the moment of entry, not at the year-end.

What the check digit does

The check digit is calculated from the preceding digits by a formula and written at the very end of the code. When a scanner or program reads the code, it recomputes the formula from the digits and compares the result with the last digit. If they differ, the code is treated as wrong: the scanner gives no beep and the software rejects the entry.

An important limit: the check digit tests the internal consistency of the code, not that the product is the right one. A code that belongs to a different product but is built correctly will pass every check.

Calculating it for EAN-13

The rule has four steps. As an example take the 12 digits before the check digit: 478123456789.

  1. Number the digits from 1 to 12, left to right.
  2. Multiply the digits in odd positions (1, 3, 5...) by 1 and those in even positions (2, 4, 6...) by 3.
  3. Add up the results.
  4. The number that brings the sum up to the next multiple of ten is the check digit.

The sum: 4×1 + 7×3 + 8×1 + 1×3 + 2×1 + 3×3 + 4×1 + 5×3 + 6×1 + 7×3 + 8×1 + 9×3, that is 4 + 21 + 8 + 3 + 2 + 9 + 4 + 15 + 6 + 21 + 8 + 27 = 128. The next multiple of ten is 130, a difference of 2. So the full code is 4781234567892.

A quicker way: the odd-position digits add up to 32 and the even-position digits to 32, so the sum is 32 + 3×32 = 128.

Verifying a finished code

To verify, take all 13 digits: multiply the last one by 1 as well and add. The total must be divisible by 10. In our example 128 + 2 = 130, so the code is valid. If the last digit were 3, the total would be 131 and the code would be rejected.

The rule works for codes of other lengths too (GTIN-8, UPC-A, GTIN-14, even the 18-digit SSCC). The general method: starting from the check digit and moving left, multiply the next digit by 3, then by 1, then by 3 again, and so on.

Which errors it catches

Imagine a storekeeper types 4781234557892 instead of 4781234567892 (a 5 in place of the 6 in position 9). That position has weight 1, so the sum falls by 1: 129 instead of 130. It is not divisible by 10, and the software rejects the code. Now swap the 5 and 6: 4781234657892. The pair gives 6×3 + 5×1 = 23 instead of 21, the sum becomes 132, and the code is rejected again.

In general the check handles errors like this:

  • One wrong digit is always caught. The weight is 1 or 3, so the sum necessarily changes.
  • Two adjacent digits swapped is caught almost always. The sum is unchanged only when the digits differ by 5 (for example 27 instead of 72), and that mistake slips through.
  • Random errors in several digits are caught in roughly 90 percent of cases. In other words, one random wrong code in ten can pass the check.

On the other hand, an extra or missing digit changes the length of the code, which a length check catches easily.

Using it in the warehouse

It helps in daily work too:

  • When you load codes from a list (Excel, 1C), verify the check digit in every row: the wrong rows point to an entry error.
  • For codes entered by hand, add a check to the software or have a second person double-check.
  • If a code read by the scanner fails the check, the cause is usually the label quality or a wrongly composed code.

Code 128 has a check character too (calculated modulo 103), but the label software adds it, so there is no need to work it out by hand.

Conclusion

One simple calculation lets the check digit catch most code errors early. During counts and labelling the Shtrix team builds and verifies internal codes by this rule, so that the label matches the number in your database.