note 88061 deleted from function.round by danbrown

From: Date: Sun, 17 May 2009 16:10:43 +0000
Subject: note 88061 deleted from function.round by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154762@lists.php.net to get a copy of this message
Note Submitter: domenicospina(A.T.)hotmail.com ---- Many people write and say wrong. If round function do something wrong, user custom function that try to solve bugs, using integrate round function, are even more wrong!!! Only way to solve this issue if you want round float numbers to any precision, is casting number as string and working with strings, using "UMAN" algorithm!!! If you want, try my simply soluction that work with any precision (i'm using it in my erp soft) <?php function arrotonda($number,$approx=0) { $number = sprintf("%s",sprintf("%f",$number)); $parts = explode(".",$number); $integers = $parts[0]; $decimals = $parts[1]; if (strpos($integers,"-")!==false){ $integers = str_replace("-","",$integers); $negative = true; } if($approx<1) { if ($decimals{0}>=5) $integers++; return $integers; } else{ $float = explode(".",($decimals/pow(10,strlen($decimals)))*pow(10,$approx)); $my_decimals = $float[0]; if($decimals{$approx}>=5) $my_decimals++; $total_decimals = ($my_decimals/pow(10,$approx)); } $my_number = sprintf("%s",sprintf("%f",($integers+$total_decimals))); $padding = (strpos($my_number,".")+1)+$approx; $my_number = str_pad($my_number,$padding,"0"); $my_number = substr($my_number,0,strpos($my_number,".")+$approx+1); $my_number = $negative?("-".$my_number):$my_number; return $my_number; } ?>

« previous php.notes (#154762) next »