note 88061 added to function.round

From: Date: Thu, 08 Jan 2009 10:45:43 +0000
Subject: note 88061 added to function.round
Groups: php.notes 
Request: Send a blank email to php-notes+get-148940@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 213.92.11.19 Probable Submitter: 89.189.36.29 ---- Manual Page -- http://www.php.net/manual/en/function.round.php Edit -- https://master.php.net/note/edit/88061 Del: integrated -- https://master.php.net/note/delete/88061/integrated Del: useless -- https://master.php.net/note/delete/88061/useless Del: bad code -- https://master.php.net/note/delete/88061/bad+code Del: spam -- https://master.php.net/note/delete/88061/spam Del: non-english -- https://master.php.net/note/delete/88061/non-english Del: in docs -- https://master.php.net/note/delete/88061/in+docs Del: other reasons-- https://master.php.net/note/delete/88061 Reject -- https://master.php.net/note/reject/88061 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148940) next »