createMod10(number)
Last updated September 09, 2009
Version: 2 | Requires: CF5 | 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:
#createMod10(123456789)#
Parameters:
Name | Description | Required |
---|---|---|
number | Value to format. | Yes |
Full UDF Source:
/**
* 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 );
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016