createMod10(number)
Last updated September 9, 2009
Version: 2 | Requires: ColdFusion 5 | Library: StrLib
Description:
This is an implementation of the Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm. It calculates the check digit for a number and appends it to the end of the orignal string.
For more details check out http://en.wikipedia.org/wiki/Luhn_algorithm
Return Values:
Returns a number.
Example:
Parameters:
| Name | Description | Required |
|---|---|---|
| number | Value to format. | Yes |
Full UDF Source:
<cfscript>
/**
* Implementation of Luhn algorithm or Mod10.
* v2 fix by Mike Causer
*
* @param number Value to format. (Required)
* @return Returns a number.
* @author Lucas Sherwood (lucas@thebitbucket.net)
* @version 2, September 9, 2009
*/
function createMod10(number) {
// this function generates the check digit and appends it to the orignal string
var nDigits = len(arguments.number);
var sum = 0;
var i=0;
var digit = "";
var checkdigit = 0;
for (i=0; i LT nDigits; i=i+1) {
digit = mid(arguments.number, nDigits-i, 1);
if(NOT (i MOD 2)) {
digit = digit+digit;
// check to see if we should add
if(len(digit) gt 1) {
digit = left(digit,1) + right(digit,1);
}
}
checkdigit = checkdigit + digit;
}
// divid by 10 and subtract from 10
checkdigit = 10 - (checkdigit mod 10);
return arguments.number & right( checkdigit, 1 );
}
</cfscript>
Search CFLib.org
Latest Additions
Tayo Akinmade added
arrayTrim
10 day(s) ago
Will Belden added
longTime
16 day(s) ago
James Sleeman added
quickSort
26 day(s) ago
Ben Forta added
GetHostAddress
29 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)