GS1 fixed-length numeric identification keys place a check digit at the end. To calculate it, work on the preceding data digits from right to left: multiply the rightmost by 3, the next by 1, and continue alternating. The final digit makes the weighted total divisible by ten.
Anchor the weights at the right edge
Exclude the check digit position while generating. Starting with the rightmost data digit, use weights 3, 1, 3, 1 and so on. Anchoring at the right matters because identifier lengths differ. In validation mode, separate the supplied final digit first and apply exactly the same sequence to the remaining body.
Complete the sum to a multiple of ten
Add every digit-times-weight product. The check digit is (10 − sum mod 10) mod 10. The outer modulo handles a sum already divisible by ten: its check digit is 0, not 10. Appending the calculated digit therefore makes the full weighted result end in zero.
Keep leading zeroes and exact length
An identifier is a digit string, not an ordinary integer. Removing a leading zero changes its declared length and can shift every alternating weight. Reject letters, signs, decimal separators, missing digits, and extra digits instead of silently repairing the input.