UPC function sum of even and odd number Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

UPC function sum of even and odd number

21 Jan 2023, 21:00

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
}
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: UPC function sum of even and odd number

21 Jan 2023, 21:22

Corrected mod and it works

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

should be

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

:dance:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: kaka2 and 138 guests