Page 1 of 1
checkdigit in banks
Posted: Mon Mar 03, 2003 6:58 am
by micknic
This is the code I use:
$av = substr($_POST["cs3"], 3);
$verifstruc = "$_POST[cs1]$_POST[cs2]$_POST[cs3]";
$av2 = substr($verifstruc, 0, 10);
$check = ($av2)%97;
if $_POST["cs1"] = 361
$_POST["cs2"] = 4000
$_POST["cs3"] = 32328 then $av should be 28
Then $check should be also 28 but it's not the case. Does someone know why???
kindest regards

Posted: Mon Mar 03, 2003 7:21 am
by Stoker
Something wrong in your formula spec here, you say $av should be '28' (and I assume match $check), you created $av on the last 3 characters of cs3 which would be 328
Concatenation is done with dot (.), makes litttle sense to put a bunch of vars by themselves inside double quotes..
Posted: Mon Mar 03, 2003 7:28 am
by daven
substr('32328',3) returns '28'. The syntax is:
string substr ( string string, int start [, int length] )
so he is getting the correct results for $av. The problem is in $av2. What should be done is
Code: Select all
// if $verifstruc is 368400032328
$av2=substr($verifstruc,10,2); // start at position 10, get 2 characters
I do agree with you on using dots rather than putting the variables inside double quotes.
Posted: Mon Mar 03, 2003 7:41 am
by Stoker
oops my bad, sorry about that, some old bill gatish mid() stuff in my head I guess
Posted: Tue Mar 04, 2003 1:56 pm
by daven
Modulus division (ex: 9%5) seems to choke when you get to anything greater than 2100000000. So 2100000000%97 will work, but 2200000000%97 will not. I have no clue as to why this is.
However, integer division (ex: 9/5) will work at higher numbers. One way to work around the modulus problem is to do something like the following:
Code: Select all
<?php
$test_val=3614000323; // initial value (from $verifstruc)
$div=97; // divisor
$res=$test_val/$div; //result
$res_int=settype($res, "integer"); //type-cast to integer (gets rid of decimal point)
$dec=$res-$res_int; // get the stuff beyond the decimal point (0.2887)
$fin=$div*$dec; // multiply the decimal stuff and the divisor
$end=$round($fin,0); // round to the nearest integer
?>
This does give the correct value ( 28 ), but it is very ugly.
Posted: Wed Mar 05, 2003 8:44 am
by daven
Slight edit to the previous function I posted. I was thinking in a hard-typed style. The settype() call will not produce the desired results in PHP. Use floor() instead. This could stand to be cleaned up a bit, but it works.
Code: Select all
<?php
// manually set since I am not POSTing anything
// set as strings, since $_POST vars are always strings
$CS1='361';
$CS2='4000';
$CS3='32328';
$rem=remainder($CS1,$CS2,$CS3);
echo $rem; # 28.
function remainder($CS1,$CS2,$CS3){
// use dot concatenation instead of quotes
$struc = $CS1.$CS2.$CS3;
#echo $struc; # output: 361400032328
// gets chars 1-10
$verifstruc=substr($struc, 0, 10);
#echo $verifstruc; # 3614000323
$div=97;
#echo $div; # 97
// float division
$res=$verifstruc/$div;
#echo $res; # 37257735.2887
// round down
$res_int=floor($res);
#echo $res_int; # 37257735
// get fractional output
$dec=$res-$res_int;
#echo $dec; # 0.288659796119
// get the remainder
$che=$div*$dec;
#echo $che; # 28.0000002235
// make the remainder an integer
$check=round($che,0);
#echo $check; # 28
return $check;
}
?>