public static int GetMod10Digit(string data) { int sum = 0; bool odd = true; for (int i = data.Length - 1; i >= 0; i--) { if (odd == true) { int tSum = Convert.ToInt32(data[i].ToString()) * 2; if (tSum >= 10) { string tData = tSum.ToString(); tSum = Convert.ToInt32(tData[0].ToString()) + Convert.ToInt32(tData[1].ToString()); } sum += tSum; } else sum += Convert.ToInt32(data[i].ToString()); odd = !odd; } return (((sum / 10) + 1) * 10) - sum; }
public static int GetMod10Digit(string data) { int sumOfDigits = data.Where((e) => e >= '0' && e <= '9') .Reverse() .Select((e, i) => ((int)e - 48) * (i % 2 == 0 ? 1 : 2)) .Sum((e) => e / 10 + e % 10); //// If the final sum is divisible by 10, then the credit card number is valid. If it is not divisible by 10, the number is invalid. return sumOfDigits % 10; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)