Created
March 5, 2013 12:02
-
-
Save pud/5089861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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