Modulus 10 check digit calculation

I try to calculate the check digit using Modulus 10 formula.
I get the correct check digit testing this against all the samples I find on the net.
However, the Bank supplied calculator shows a different result 90% of the time.
Can the math geniuses among you see anything wrong with my code or is the Bank
lying that they use Modulus 10 to calculate the check digit.
Here is my code - ignore the unnecessary control steps
Once I get this right I will move to faster in memory calculation

Sub Action()
dim i,D, M, R, CD,x, y, TM as integer
dim arr As string

Listbox1.DeleteAllRows

for i = 1 to len(SixDigit.Text)
Listbox1.AddRow mid(SixDigit.Text, i,1)
next

//// doubling every second digit
// moving from the right to the left
x = 1
for i = len(SixDigit.Text) -1 DownTo 0
D = val(Listbox1.Cell(i,0)) // = the single integer
x = x + 1
y = x mod 2
if x mod 2 = 0 then // only double the value of every second digit
Listbox1.Cell(i,1) = str(d * 2)
M = d * 2
else
Listbox1.Cell(i,1) = Listbox1.Cell(i,0)
M = d
end if
R = M
Listbox1.Cell(i,2) = str®
if R > 9 then
R = val(Listbox1.Cell(i,2)) -9
Listbox1.Cell(i,2) = str®
end if
cd = cd + R
next
TM = cd * 9
Division.Text = str™
Result.Text = str(cd)
arr = str™
CheckDigit.Text = Right(arr,1)
NewCRN.Text = SixDigit.Text + CheckDigit.Text
End Sub

Any help would be much appreciated

Gerd

It would help us out if you wrapped your code in the CODE tags for readability and gave use some reference for the formula you are trying to implement. I think you mean this one.

Here is my version. Please compare it to yours. I’ve validated the results as best I could:

Public Function Modulus10(digits As String) as String
  //
  // Confirm the digits are really, well, digits
  //
  
  dim validator as new RegEx
  validator.SearchPattern = "\\A\\d+\\z"
  if validator.Search( digits ) is nil then
    //
    // Raise an exception or something
    //
  end if
  
  static doublingTable() as string = array( "0", "2", "4", "6", "8", "1", "3", "5", "7", "9" )
  
  dim digitArr() as string = digits.Split( "" )
  for i as integer = digitArr.Ubound downto 0 step 2
    digitArr( i ) = doublingTable( digitArr( i ).Val )
  next
  
  dim sum as integer
  for each digit as string in digitArr
    sum = sum + digit.Val
  next
  
  dim check as integer = ( sum * 9 ) mod 10
  return str( check )
End Function

I added this function long time ago and it works fine, but I do not remember why …

[code]Public Function SubModulo10(value As String) as String
Dim i,r As Integer
Dim s As String

r=0
for i=1 to len(value)
s=Mid(value,i,1)
if InStr(“0123456789”,s)>0 then
r=Val(Mid(Mid(“0946827135094682713”,r+1,10),Val(s)+1,1))
end if
next

if r=0 then
Return “0”
Else
Return Str(10-r)
end if

End Function[/code]

Wow. I’m… well… that’s… wow.

Hi Kem

my apologies for not using the code tag. When I pasted my code into the blog, it looked ok.
Here it is

[code] dim i,D, M, R, CD,x, y, TM as integer
dim arr As string
Listbox1.DeleteAllRows

for i = 1 to len(SixDigit.Text)
Listbox1.AddRow mid(SixDigit.Text, i,1)
next

//// doubling every second digit
// moving from the right to the left
x = 1
for i = len(SixDigit.Text) -1 DownTo 0
D = val(Listbox1.Cell(i,0)) // = the single integer
x = x + 1
y = x mod 2
if x mod 2 = 0 then // only double the value of every second digit
Listbox1.Cell(i,1) = str(d * 2)
M = d * 2
else
Listbox1.Cell(i,1) = Listbox1.Cell(i,0)
M = d
end if
R = M
Listbox1.Cell(i,2) = str®
if R > 9 then
R = val(Listbox1.Cell(i,2)) -9
Listbox1.Cell(i,2) = str®
end if
cd = cd + R
next
TM = cd * 9
Division.Text = str™
Result.Text = str(cd)
arr = str™
CheckDigit.Text = Right(arr,1)
NewCRN.Text = SixDigit.Text + CheckDigit.Text[/code]

In the meantime I have learned that the Bank uses two versions of Modulus 10
One the un-adultered one (which I tried to code) and a modified one which I compared my results to.

I can report that Kem’s and my versions work.

Alain’s version is intriguing, but does not work as is.
I do not understand the meaning of the string

 r=Val(Mid(Mid("0946827135094682713",r+1,10),Val(s)+1,1))

Is there any mathematical value to the sequence of numbers?