Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Forms
Hi all,
 
I've been trying to make a mod10 check digit calculator in C#. I've found a snippet that works, however, I cannot seem to find out how to tell the program to return 0 if the check digit is 10. It works for all other numbers except the ones that return 10. Can somebody please help me with where to insert this?
 
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;
        }
Posted 21-Feb-11 1:27am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the Modulus operator: "x % 10" will return 0 if x == 10.
  Permalink  
Comments
Jaykay832 at 21-Feb-11 6:46am
   
Thank you for the suggestion. Could you please show me where I need to set this in the code?
OriginalGriff at 21-Feb-11 6:50am
   
Since mod10 is supposed to always return a number if the range 0 to 9, I would recommend you think about the "return" statement?
 
Possibly change it to:
 
int result = (((sum / 10) + 1) * 10) - sum;
return result % 10;
 
Just a thought...
Jaykay832 at 21-Feb-11 6:53am
   
Works perfect! Thank you so much.
OriginalGriff at 21-Feb-11 6:55am
   
Welcome!
SAKryukov at 21-Feb-11 21:50pm
   
5, Your Patience,
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try this,
 
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;
}
 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100