Page 1 of 1

UPC function sum of even and odd number

Posted: 21 Jan 2023, 21:00
by maitresin
The below code should calculate all the odd number of 06420011589 and multiply by 3, then calculate the even number. Add the odd number total 57 to the even number total 17 to make 74. Then you need to round up to the next 10s value and make the difference between it and the 74. 80 - 74 should return 6 which is the checkdigit number.
Correct UPC should be 064200115896 but in the below calculation it return 06420011589-360010

How could I make correctly the odd and even and checkdigit part of the code?

Code: Select all

ProductCode := 11589
UPC := UPC_A(ProductCode)
msgbox, %upc%

UPC_A(ProductCode) {
    Suffix := "064200"
    UPC := Suffix . ProductCode
    OddSum := 0
    EvenSum := 0
    Loop, 11
    {
        if(A_Index mod 2 = 1)
            OddSum += SubStr(UPC, A_Index, 1)
        else
            EvenSum += SubStr(UPC, A_Index, 1)
    }
    CheckDigit := (10 - ((3*OddSum + EvenSum) mod 10)) mod 10
    if(CheckDigit = 10) {
        CheckDigit := 0
    }
    UPC .= CheckDigit
	
    return UPC
}

Re: UPC function sum of even and odd number  Topic is solved

Posted: 21 Jan 2023, 21:04
by mikeyww

Re: UPC function sum of even and odd number

Posted: 21 Jan 2023, 21:22
by maitresin
Corrected mod and it works

CheckDigit := (10 - ((3*OddSum + EvenSum) mod 10)) mod 10

should be

CheckDigit := mod((3*OddSum + EvenSum),10)

:dance: