Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

tep_round FYI


guntersammet

Recommended Posts

Hi all:

Just stumbled over an issue that tep_round does not format negative numbers correctly. Here a fix to this issue:

 


// Wrapper function for round()
 function tep_round($number, $precision) {
   $negative_number = false;
   if($number < 0){
     $negative_number = true;
     $number = $number * -1;
   }
   if (strpos($number, '.') && (strlen(substr($number, strpos($number, '.')+1)) > $precision)) {
     $number = substr($number, 0, strpos($number, '.') + 1 + $precision + 1);
     if (substr($number, -1) >= 5) {
       if ($precision > 1) {
         $number = substr($number, 0, -1) + ('0.' . str_repeat(0, $precision-1) . '1');
       } elseif ($precision == 1) {
         $number = substr($number, 0, -1) + 0.1;
       } else {
         $number = substr($number, 0, -1) + 1;
       }
     } else {
       $number = substr($number, 0, -1);
     }
   }
   if($negative_number == true){
     $number = $number * -1;
   }

   return $number;
 }

 

HTH

 

Gunter

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...