12 cifret tal - delt op i 12 variabler
Hejsa,Jeg skal tjekke om en EAN13-stregkode er valid, og skal gøre følgende ved den 12-cifrede stregkode:
Add the digits (up to but not including the check digit) in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.
Add the digits (up to but not including the check digit) in the even-numbered positions (second, fourth, sixth, etc.) to the result.
If the last digit of the result is 0, then the check digit is 0.
If the last digit of the result is not zero, then subtract the last digit from 10. The answer must equal the check digit.
Fra: http://en.wikipedia.org/wiki/Check_digit
checkdigit = 10-((digit2+digit4+digit6+digit8+digit10)*3)+(digit1+digit3+digit5+digit7+digit9+digit11)
if(checkdigit==10){checkdigit=0;}
000000000222 tjekkes altså således:
((0+0+0+0+0+2)*3)+(0+0+0+0+0+2) = 8
Tallet trækkes fra 10, hvis tallet ikke er nul, hvilket i ovenstående tilfælde giver 2, som er det sidste nummer i stregkoden, hvilket betyder at den er valid.
Hvordan gør jeg det?
