2014年3月12日水曜日

C#でモジュラス10/ウェイト2の計算

  1. /// <summary>  
  2. /// モジュラス10/ウェイト2 計算  
  3. /// NW-7  
  4. /// </summary>  
  5. /// <param name="Value">/// <returns></returns>  
  6. public static string GetModulus10Weight2(string Value)  
  7. {  
  8.     bool result = Regex.IsMatch(Value, @"^[0-9]+$");  
  9.     if (!result)  
  10.     {  
  11.         return null;  
  12.     }  
  13.   
  14.     int checkDigit = 0;  
  15.   
  16.     for (int i = 0; i < Value.Length; i++)  
  17.     {  
  18.         checkDigit += int.Parse(Value.Substring(Value.Length - 1 - i, 1)) * (i % 2 == 0 ? 2 : 1);  
  19.     }  
  20.   
  21.     checkDigit = 10 - (checkDigit % 10);  
  22.   
  23.     checkDigit = checkDigit == 10 ? 0 : checkDigit;  
  24.   
  25.     return checkDigit.ToString();  
  26. }  

0 件のコメント:

コメントを投稿