Created
April 29, 2014 14:38
-
-
Save martinusso/11402319 to your computer and use it in GitHub Desktop.
Check Digit based on modulo 11 - c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!