gpt4 book ai didi

PHP CUSIP 校验码

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:51:10 37 4
gpt4 key购买 nike

我需要能够根据他们的校验位来验证 CUSIP 号码。多亏了维基百科,我有了这个过程的伪代码,但到目前为止我还无法在 PHP 中复制它。

可以找到伪代码here .

我的PHP:

    <?php
/**
* function to return the check digit value of a cusip
* @param $cusip
* the cusip for processing.
* @return Int
* cusip check digit.
*/
function cusip_checksum($cusip){
$sum = 0;
$rebuiltcusip = '';
for($i = 1; $i <= 8; $i++){
$c = substr($cusip, ($i - 1), 1); //$i needs to be 0, so as we start at 1, take 1 off.
$rebuiltcusip .= $c;
switch(true){
case $c == '0': // ctype_digit(0) returns false, so checking for 0 here.
$v = $c;
watchdog("case 0: ", $v);
break;
case ctype_digit($c): //check if numeric
$v = $c;
watchdog("case ctype_digit: ", $v);
break;
case $c == '*':
$v = 36;
watchdog("case *: ", $v);
break;
case $c == '@':
$v = 37;
watchdog("case @: ", $v);
break;
case $c == '#':
$v = 38;
watchdog("case #: ", $v);
break;
case !ctype_digit($c): //check letter last as this check would pass with * @ or # so allow them to be checked first
$v = (ord($c) - 64) + 9; //set ordinal number, -64 as this returns ASKII value, then add 9.
watchdog("case not ctype_digit: ", $v);
break;
}
if(($i % 2) == 0){ //check if odd
$v = $v * 2;
watchdog("case odd: ", $v);
}

$sum = $sum + ($v / 10) + ($v % 10);
watchdog("sum end loop: ", $sum);
}

$ncd = (10 - ($sum % 10)) % 10;
$rebuiltcusip .= $ncd;
watchdog("rebuilt cusip: ", "Cusip: ".$cusip." Rebuilt: ".$rebuiltcusip);
return $ncd;
}
?>

watchdog 只是我记录过程。

传入校验位值为 8 的 CUSIP:98986T108,实际返回值为 98986T104(校验位值为 4)。

所以:

<?php
print cusip_checksum('98986T108');
?>

应该返回 8,它返回 4。

有人能找出原因吗?

最佳答案

将除法包装在 floor() 函数中,您就在那里:

$sum = $sum + floor($v / 10) + ($v % 10);

现在 CUSIP 中的 Q 的值为 26。这会将 2 和 6 添加到 $sum 而不是添加 2.6 和 6。

这是经过更正的最终函数。我已经通过路透社提取的 17614 个 CUSIP 运行了这个,有 11 个不匹配。这是我通常从路透社数据中看到的错误级别,因此我对例程有信心。

/**
* function to return the check digit value of a cusip
* @param $cusip
* the cusip for processing.
* @return Int
* cusip check digit.
*/
function cusip_checksum($cusip){
$sum = 0;
$rebuiltcusip = '';
for($i = 1; $i <= 8; $i++){
$c = substr($cusip, ($i - 1), 1); //$i needs to be 0, so as we start at 1, take 1 off.
$rebuiltcusip .= $c;
switch(true){
case $c == '0': // ctype_digit(0) returns false, so checking for 0 here.
$v = $c;
watchdog("case 0: ", $v);
break;
case ctype_digit($c): //check if numeric
$v = $c;
watchdog("case ctype_digit: ", $v);
break;
case $c == '*':
$v = 36;
watchdog("case *: ", $v);
break;
case $c == '@':
$v = 37;
watchdog("case @: ", $v);
break;
case $c == '#':
$v = 38;
watchdog("case #: ", $v);
break;
case !ctype_digit($c): //check letter last as this check would pass with * @ or # so allow them to be checked first
$v = (ord($c) - 64) + 9; //set ordinal number, -64 as this returns ASKII value, then add 9.
watchdog("case not ctype_digit: ", $v);
break;
}
if(($i % 2) == 0){ //check if odd
$v = $v * 2;
watchdog("case odd: ", $v);
}

$sum = $sum + floor($v / 10) + ($v % 10);
watchdog("sum end loop: ", $sum);
}

$ncd = (10 - ($sum % 10)) % 10;
$rebuiltcusip .= $ncd;
watchdog("rebuilt cusip: ", "Cusip: ".$cusip." Rebuilt: ".$rebuiltcusip);
return $ncd;
}

关于PHP CUSIP 校验码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21828443/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com