note 76999 added to function.ceil

From: Date: Thu, 09 Aug 2007 14:28:29 +0000
Subject: note 76999 added to function.ceil
Groups: php.notes 
Request: Send a blank email to php-notes+get-129375@lists.php.net to get a copy of this message
the fCeil and round_up listed below are not reliable. This could be due to a broken ceil function: CODE: function fCeil($val,$pressision=2){ $p = pow(10,$pressision); $val = $val*$p; $val = ceil($val); return $val /$p; } print "fCeil: ".fCeil("0.5500",2)."\n"; print "ceil: ".ceil("55.00")."\n"; print "ceil: ".ceil(0.5500 * 100)."\n"; // should be interpreted as ceil(55); OUTPUT: fCeil: 0.56 ceil: 55 ceil: 56 Tested: PHP v5.2.2, v5.1.6, v5.0.4 CLI Quick function I used as replacement: CODE: function round_up ($value, $precision=2) { $amt = explode(".", $value); if(strlen($amt[1]) > $precision) { $next = (int)substr($amt[1],$precision); $amt[1] = (float)(".".substr($amt[1],0,$precision)); if($next != 0) { $rUp = ""; for($x=1;$x<$precision;$x++) $rUp .= "0"; $amt[1] = $amt[1] + (float)(".".$rUp."1"); } } else { $amt[1] = (float)(".".$amt[1]); } return $amt[0]+$amt[1]; } print round_up("0.5500",2)."\n"; print round_up("2.4320",2)."\n"; print "\nprecision: 2\n"; print round_up("0.5",2)."\n"; print round_up("0.05",2)."\n"; print round_up("0.050",2)."\n"; print round_up("0.0501", 2)."\n"; print round_up("0.0500000000001", 2)."\n"; print "\nprecision: 3\n"; print round_up("0.5",3)."\n"; print round_up("0.05",3)."\n"; print round_up("0.050",3)."\n"; print round_up("0.0501",3)."\n"; print round_up("0.0500000000001",3)."\n"; OUTPUT: 0.55 2.44 precision: 2 0.5 0.05 0.05 0.06 0.06 precision: 3 0.5 0.05 0.05 0.051 0.051 ---- Server IP: 64.71.164.2 Probable Submitter: 216.254.39.226 ---- Manual Page -- http://www.php.net/manual/en/function.ceil.php Edit -- https://master.php.net/note/edit/76999 Del: integrated -- https://master.php.net/note/delete/76999/integrated Del: useless -- https://master.php.net/note/delete/76999/useless Del: bad code -- https://master.php.net/note/delete/76999/bad+code Del: spam -- https://master.php.net/note/delete/76999/spam Del: non-english -- https://master.php.net/note/delete/76999/non-english Del: in docs -- https://master.php.net/note/delete/76999/in+docs Del: other reasons-- https://master.php.net/note/delete/76999 Reject -- https://master.php.net/note/reject/76999 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129375) next »