Tuesday, February 14, 2012

Calculating Luhn Digit in Crystal Reports

I have an 18 digit number and i need a SQL formula in Crystal Reports to calculate the Luhn digit.

Below is how Luhn is calculated.

Step 1: Double the value of alternate digits beginning with the first right hand digit.

e.g 8 6 3 7 5 = 8 12 3 14 5

Step 2: Add the individual digits compromising the products obtained in Step 1 to each of the unaffected digits in the original number.

e.g 8 12 3 14 5 = 8+1+2+3+1+4+5 (total=24)

Step 3: Subtract the total obtained in Step 2 from the next higher number ending in 0.

e.g 24 = 30-24 = 6 (Luhn = 6!)

Any help here would be fantastic!!This should do it...

Local StringVar sValue := {test.NUMBER};

Local NumberVar nSubtotal;
Local NumberVar i;

For i := (Length(sValue) - 1) To 1 Step -2 Do
(nSubtotal := nSubtotal + CDbl(sValue[i]));

For i := Length(sValue) To 1 Step -2 Do
nSubtotal := nSubTotal + (If CDbl(sValue[i]) < 5
Then CDbl(sValue[i]) * 2
Else
CDbl(sValue[i]) - (9 - CDbl(sValue[i])));

nSubtotal Mod 10;

No comments:

Post a Comment