note 46259 deleted from function.floor by tomsommer
| From: | tomsommer@php.net | Date: | Tue, 05 Oct 2004 17:54:03 +0000 |
| Subject: | note 46259 deleted from function.floor by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78037@lists.php.net to get a copy of this message | ||
Note Submitter: silicate at codenaked dot org
----
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