|
// Posted by: wapnethr
// Explanation of Luhn's algorithm: http://encyclopedia.thefreedictionary.com/Luhn%20algorithm
using System;
namespace System.Math
{
/// <summary>
/// Class for calculating checksum number using Luhn's algorithm.
/// </summary>
public class LuhnAlgorithm
{
private LuhnAlgorithm(){}
public static int GetLuhnAlgorithmNumber(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;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.