Skip to content

Instantly share code, notes, and snippets.

@lucamauri
Last active January 14, 2016 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucamauri/d05b25f1c0f7a59e4c2f to your computer and use it in GitHub Desktop.
Save lucamauri/d05b25f1c0f7a59e4c2f to your computer and use it in GitHub Desktop.
Check digit Luhn
Public Function Luhn(StringToCheck As String) As String
Dim CurrDigit As Integer
Dim TotalCounter As Integer
Dim DoubleDigit As Integer
TotalCounter = 0
For i = StringToCheck.Length To 1 Step -1
Try
CurrDigit = Integer.Parse(StringToCheck(i - 1))
Catch ex As Exception
Return ex.ToString
End Try
If Not CBool(i Mod 2) Then
DoubleDigit = CurrDigit * 2
If DoubleDigit > 9 Then
TotalCounter += Integer.Parse(DoubleDigit.ToString()(0)) + Integer.Parse(DoubleDigit.ToString()(1))
Else
TotalCounter += DoubleDigit
End If
Else
TotalCounter += CurrDigit
End If
Next
Return ((TotalCounter * 9) Mod 10).ToString
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment