note 106818 deleted from function.round by googleguy

From: Date: Sun, 09 Dec 2012 08:14:16 +0000
Subject: note 106818 deleted from function.round by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192409@lists.php.net to get a copy of this message
Note Submitter: James Dudeck ---- Rounding decimal points <?php function rfloor($real,$decimals = 2) { return substr($real, 0,strrpos($real,'.',0) + (1 + $decimals)); } function rceil($real,$decimals = 2) { $newreal = substr($real, 0,strrpos($real,'.',0) + (1 + $decimals)); if($real[strlen($newreal)] > 0) { $newreal[strlen($newreal) - 1] += 1; } return $newreal; } function rround($real,$decimals = 2,$halfdown = FALSE) { $newreal = substr($real, 0,strrpos($real,'.',0) + (1 + $decimals)); if(($halfdown == FALSE) && ($real[strlen($newreal)] = 5)) { $newreal[strlen($newreal) - 1] += 1; } else if($real[strlen($newreal)] > 5) { $newreal[strlen($newreal) - 1] += 1; } return $newreal; } ?> Usage: rceil(12.12146321,2); //12.13 -- as long as the value is greater than 0, round up. rfloor(12.12946321,2); //12.12 -- drop everything after the number of decimal places rround(12.12546321,2); //12.13 -- round half up rround(12.12546321,2,TRUE); //12.12 -- round half down

« previous php.notes (#192409) next »