jch Posted March 18, 2007 Posted March 18, 2007 Hello, i have noticed a weird problem with tep_round Could you test this : in fact for some i have a dot at the end of the number. <?php function tep_round($number, $precision) { 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); } } return $number; } echo tep_round('10.4436210893',0).'<br />'; echo round('10.4436210893','0').'<br /><br />'; echo tep_round('6.44628099174',0).'<br />'; echo round('6.44628099174','0').'<br /><br />'; echo tep_round('18.7155508763',0).'<br />'; echo round('18.7155508763','0').'<br /><br />'; ?> RESULT 10[color="#FF0000"].[/color] 10 6[color="#FF0000"].[/color] 6 19 19
Guest Posted March 18, 2007 Posted March 18, 2007 nice one, use this one function tep_round($n, $d = 0) { $n = $n - 0; if ($d === NULL) $d = 2; $f = pow(10, $d); $n += pow(10, - ($d + 1)); $n = round($n * $f) / $f; $n += pow(10, - ($d + 1)); $n += ''; if ( $d == 0 ) return substr($n, 0, strpos($n, '.')); else return substr($n, 0, strpos($n, '.') + $d + 1); } It's from a post in php.net, hth
Guest Posted March 18, 2007 Posted March 18, 2007 or this one here the other one I've seen has precision problems: function tep_round($number, $round=2) { $tempd = $number*pow(10,$round); $tempd1 = round($tempd); $number = $tempd1/pow(10,$round); return $number; } its from php.net again
jch Posted March 18, 2007 Author Posted March 18, 2007 nice one this is not mine. this is from oscommerce in includes/general.php that why i have post this, to see if it's not a bug of oscommerce ;) thanks
Guest Posted March 18, 2007 Posted March 18, 2007 this is not mine.this is from oscommerce in includes/general.php that why i have post this, to see if it's not a bug of oscommerce ;) thanks yes I understand, I don't know perhaps there is already a bug report for it, haven't checked, I just searched php.net a bit after testing your report and posted these alternatives. They seem to work.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.