Created
November 14, 2013 09:20
-
-
Save yoshikazuendo/7463873 to your computer and use it in GitHub Desktop.
モジュラス10/ウェイト3のチェックデジットを取得する。てきとーにつくったもの。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// モジュラス10/ウェイト3のチェックデジットを取得します。 | |
/// </summary> | |
/// <param name="target">対象の文字列</param> | |
/// <returns>チェックデジット</returns> | |
private static string GetChkDigitM10W3(string target) | |
{ | |
// 奇数桁は3倍、偶数桁は1倍にしてサマリーをとる。 | |
int sum = 0; | |
for (int i = 0; i < target.Length; i++) { | |
if (i % 2 == 0) { | |
// 偶数桁 | |
sum = sum + (int.Parse(target[i].ToString()) * 3); | |
} else { | |
// 奇数桁 | |
sum = sum + (int.Parse(target[i].ToString()) * 1); | |
} | |
} | |
// サマリーを10で割った余りを計算。 | |
int notMuch = sum % 10; | |
if (notMuch == 0) { | |
// 余りが0の場合は0を返す。 | |
return 0.ToString(); | |
} else { | |
// 0ではない場合は10で引いた値を返す。 | |
return (10 - notMuch).ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment