CFLib.org – Common Function Library Project

createMod10(number)

Last updated September 9, 2009

author

Lucas Sherwood

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 );
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson