note 46259 added to function.floor

From: Date: Tue, 05 Oct 2004 01:24:05 +0000
Subject: note 46259 added to function.floor
Groups: php.notes 
Request: Send a blank email to php-notes+get-78000@lists.php.net to get a copy of this message
Hey, just a different version of the penny_rounder by Sam. Two versions of the function, one that replicates the functionality of Sams only is faster and another one that is about the same speed as Sams but pads the fraction (ie 99.1 ~ 99.10) If you are sure that the data has at least two decimal places, use the first as it's much faster - especially if generating a sales report for the last bajillion years. hehe <?php // Rounds without padding (faster) function PennyRounder($Amount) { // type casting $Amount here speeds it up some $StringAmount = (string)$Amount; return substr($StringAmount, 0, strpos($StringAmount,'.')+3); } // End PennyRounder function PaddedPennyRounder($Amount) { // type casting $Amount here speeds it up some $StringAmount = (string)$Amount; return sprintf("%.2f", substr($StringAmount, 0, strpos($StringAmount,'.')+3)); } // End PaddedPennyRounder echo PennyRounder(79.999); // echos 79.99 echo PaddedPennyRounder(79.999); //echos 79.99 echo PennyRounder(79.9); // echos 79.9 echo PaddedPennyRounder(79.9); //echos 79.90 ?> So uhh yeah. Be safe, Silicate ---- Manual Page -- http://www.php.net/manual/en/function.floor.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46259 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46259&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46259&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78000) next »