Skip to content

Instantly share code, notes, and snippets.

@martinusso
Created April 29, 2014 14:38
Show Gist options
  • Save martinusso/11402319 to your computer and use it in GitHub Desktop.
Save martinusso/11402319 to your computer and use it in GitHub Desktop.
Check Digit based on modulo 11 - c#
public class Modulus11
{
public string GetCheckDigit(string number)
{
int sum = 0;
for (int i = number.Length - 1, multiplier = 2; i >= 0; i--)
{
sum += (int)char.GetNumericValue(number[i]) * multiplier;
if (++multiplier > 9) multiplier = 2;
}
int mod = (sum % 11);
if (mod == 0 || mod == 1) return "0";
return (11 - mod).ToString();
}
}
@PeterMontgomery777
Copy link

Hi martinusso, This only seems to work for numbers with 7 digits or fewer. I think the multiplier should be set back to 2 when it exceeds 7, not 9. But its a cool solution once that is sorted out. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment