Skip to content

Instantly share code, notes, and snippets.

@pud
Created March 5, 2013 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pud/5089861 to your computer and use it in GitHub Desktop.
Save pud/5089861 to your computer and use it in GitHub Desktop.
<cffunction name="calculateUpcCheckDigit" output="no" returnType="string">
<cfargument name="upc" required="true" type="string">
<cfset oddnums = 0>
<cfset evennums = 0>
<cfloop from="1" to="#len(upc)#" index="x">
<cfif x mod 2 neq 0>
<cfset oddnums = oddnums + mid(upc, x, 1)>
</cfif>
</cfloop>
<cfloop from="1" to="#len(upc)#" index="x">
<cfif x mod 2 eq 0>
<cfset evennums = evennums + mid(upc, x, 1)>
</cfif>
</cfloop>
<cfset oddnums = oddnums * 3>
<cfset checkdigit = oddnums + evennums>
<cfset checkdigit = checkdigit mod 10>
<cfset checkdigit = 10 - checkdigit>
<cfif checkdigit eq 10>
<cfset checkdigit = 0>
</cfif>
<cfreturn checkdigit>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment