Skip to content

Instantly share code, notes, and snippets.

@yoshikazuendo
Created November 14, 2013 09:20
Show Gist options
  • Save yoshikazuendo/7463873 to your computer and use it in GitHub Desktop.
Save yoshikazuendo/7463873 to your computer and use it in GitHub Desktop.
モジュラス10/ウェイト3のチェックデジットを取得する。てきとーにつくったもの。
/// <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