0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JANコードのチェックデジットを計算する

Posted at
 JANコードのチェックデジットを計算する

protected function calculateCheckDigit($code)
{
    // JANコードの前12桁を取得
    $checkCode = substr($code, 0, 12);
    $sum = 0;
    for ($i = 0; $i < strlen($checkCode); $i++) {
        // 奇数桁は1、偶数桁は3をかけて足す
        $sum += (($i+1) % 2 == 0 ? 3 : 1) * $checkCode[$i];
    }
    // 総和の下1桁の数字を10から引く、10なら1の位は0なので、下1桁をチェックデジットとする
    $CheckDigit = (10 - $sum % 10) % 10;
    // チェックデジットが一致しない
    if ((int)$CheckDigit != (int)substr($code, 12, 1)) {
        return false;
    }
    return true;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?