note 45264 deleted from function.floor by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 Mar 2011 15:22:19 +0000 |
| Subject: | note 45264 deleted from function.floor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177764@lists.php.net to get a copy of this message | ||
Note Submitter: sam at bigroomstudios dot com
----
Here's a way to get around jolyon's 79.99 problem (below). If you want to take a float
value representing a price and drop off any fractions of a cent, use this:
<?php
function round_to_penny($amount){
$string = (string)($amount * 100);
$string_array = split("\.", $string);
$int = (int)$string_array[0];
$return = $int / 100;
return $return;
}
print( round_to_penny(79.99999) ); //result: 79.99
?>
Probably not super efficient, but get's the job done if you've already invested in using
float values.
Cheers,
Sam