I'm struggling with Luhn algorithm to calculate check digit for 14 digit length number. I found this solution here, hut it's only for 6 digit number. If I want to create it for more digits I'll get over formula compiled characters limit.
Here's formula for 6 digit number
MOD(10 - MOD(
IF(
MOD(VALUE(wir_GSAP_Inv_No__c) ,10)*2 > 9,
MOD(MOD(VALUE(wir_GSAP_Inv_No__c) ,10)*2,10) + 1,
MOD(VALUE(wir_GSAP_Inv_No__c) ,10)*2
) +
FLOOR(MOD(MOD(VALUE(wir_GSAP_Inv_No__c), 100)/10,10)) +
IF(
(FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 1000)/100)*2)>9,
MOD(FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 1000)/100)*2,10)+1,
(FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 1000)/100)*2)
) +
FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 10000)/1000) +
IF(
(FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 1000)/100)*2)>9,
MOD(FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 1000)/100)*2,10)+1,
(FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 1000)/100)*2)
) +
FLOOR(MOD(VALUE(wir_GSAP_Inv_No__c), 10000)/1000)
,10)
,10)
Any idea how can I do it?
Luhn algorithm (from wikipedia): The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This number must pass the following test:
- From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then sum the digits of the products (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9).
- Take the sum of all the digits.
- If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid. Assume an example of an account number "7992739871" that will have a check digit added, making it of the form 7992739871x.
The check digit (x) is obtained by computing the sum of digits then computing 9 times that value modulo 10 (in equation form, (67 × 9 mod 10)). In algorithm form:
- Compute the sum of the digits (67).
- Multiply by 9 (603).
- The last digit, 3, is the check digit. Thus, x=3.