Page 1 of 1

whole number/division/modulus?

Posted: Mon Dec 15, 2003 10:41 pm
by SurfScape
I have an array, I want to check for errors before placing in my db.

Code: Select all

їdata] => Array
        (
            їE001] => 40.00
            їE021] => 2.25
            їE022] => 1.16
            їE01S] => 0.00
            їE012] => 0.00
            їE09B] => 0.00
        )
Now, these values can only have possible values of x.25, x.50, x.75 , x.00 or x. so I want to do a foreach loop through the array and determine if any of these values doesn't meet that criteria. Such as the 3rd item above ( [E022] => 1.16 ].

I've tried several different approaches including:

Code: Select all

foreach( $_POSTї'data'] as $k => $v ) {
      if ( ($v % .25) > 0 )  {
        $site->raise_error( 'All data must be either .25, .50, .75, .00' );
      }
    }
Which didn't work, well I don't know why, maybe because the values are strings? also the modulus (%) doesn't seem to work with decimals.

I also tried:

Code: Select all

foreach( $_POSTї'data'] as $k => $v ) {
      $v = $v / .25;
      if ( is_double($v)  )  {
        $site->raise_error( 'All data must be either .25, .50, .75, .00' );
      }
    }
Which didn't work either, I think because a division operator automatically makes it a double or float.

If anyone can clue me in, I'd appreciate it.

-Garrett:confused:

Posted: Mon Dec 15, 2003 11:26 pm
by Weirdan
Try this:

Code: Select all

foreach($_POST['data'] as $k=>$v){
  $v=round($v,2);
  if(($v*100)%25)
      $site->raise_error( 'All data must be either .25, .50, .75, .00' );
}

Posted: Mon Dec 15, 2003 11:35 pm
by SurfScape
Here is the code, I used, works like a charm

Code: Select all

foreach ( $_POSTї'data'] as $k => $v )
     {
         $v =  sprintf( "%01.2f", $v );
         $decimals = substr($v, -2);
         if (($decimals % 25) != 0)  {
             $site->raise_error( 'All data must be either .25, .50, .75, .00.<br> See the rounding charts!' );
         &#125;
     &#125;
Thanks Weirdan for the quick reply!

-Garrett