Based on what I'm reading on wikipedia your doing the calculation incorrectly in the first example.
String.Substring
+String.Subtring
or "1" + "1" = "11". You are looking for 1+1=2. You need to convert each substring to int prior to adding.
Binary + operators are predefined for numeric and string types. For numeric types, + computes the sum of its two operands. When one or both operands are of type string, + concatenates the string representations of the operands.
String.Substring
is 0 based so you are missing theString.Substring(0,1)
for your even digits
startIndex: The zero-based starting character position of a substring in this instance.
- Your checkdigit string attempts to take the 12th digit of a string which is less than 12 digits. Also, this isn't the calculation as seen in the link. It's looking for 10 - mod 10 of that number. This is also happening in your updated edit in the line
string chkDigit = ((300 - chkDigitSubtotal).ToString()).Substring(12, 1);
You are attempting to take a Substring
from the 12th 0 based character, or 13th character for a length of 1. However, the value of chkDigitSubtotal will not be that large/long.
As far as your error, As Golden Dragon states in his answer your are converting a 12 characcter string to a number, which won't fit into an Int32
and you should use a long.
int first = Math.Abs(Convert.ToInt32(value));
goes to
long first = Math.Abs(Convert.ToInt64(value));
I would check your calculation is actually functioning logically in the previous steps first.
Update
In your edit 2 it looks like your last line has the variables around the % operator
reversed. You want to take the remainder of 109 divided by 10 (which is 9) and your code takes the remainder of 10 divided by 109 (which is 0) So change.
chkDigitSubtotal = 10 - (10 % (chkDigitEven + chkDigitOdd));
to
chkDigitSubtotal = 10 - ((chkDigitEven + chkDigitOdd) % 10);
Now 10 - 9 = 1 which is your checkDigit.