Re: Money formatting
| From: | Justin French | Date: | Wed, 07 Aug 2002 01:17:54 +0000 |
| Subject: | Re: Money formatting | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-111461@lists.php.net to get a copy of this message | ||
Dan,
I think I've got something like this floating around in my library of code:
<?
function currencySplit($cents,$zerofill = 1)
{
if(is_int($cents))
{
$whole = substr($cents, 0, strlen($cents) - 2);
$decimal = substr($cents, strlen($cents) - 2, strlen($cents));
if($zerofill && empty($whole))
{
$whole = 0;
}
$formatted = "\${$whole}.{$decimal}";
return $formatted;
}
else
{
return "error";
}
}
?>
It accepts an integer as the first argument (a dollar value in cents), and
the optional second argument specifies whether values < $1 (100) are
prefixed with a zero. It defaults to TRUE, so 99 returns $0.99 by default.
Examples:
<?
echo currencySplit(90,0)."<br />"; // returns $.90
echo currencySplit(90,1)."<br />"; // returns $0.90
echo currencySplit(90)."<br />"; // returns $0.90
// (2nd parameter defaults to TRUE)
echo currencySplit(1550)."<br />"; // returns $15.50
echo currencySplit(1550,1)."<br />"; // returns $15.50
echo currencySplit(1954250)."<br />"; // returns $19542.50
echo currencySplit(1954250,1)."<br />"; // returns $19542.50
?>
Season to taste, hack it up to suit your needs, etc etc :)
Justin French
on 07/08/02 2:55 AM, Dan McCullough (McWC@rocketmail.com) wrote:
> I need to format numbers to look like dollars and cents.
> So if I have 1550 it needs to look like $15.50 if I have 50 then it needs to
> look like $.50
> Any thoughts.
>
> =====
> --------------------------------------------------------
> "Theres no such thing as a problem unless the servers are on fire!"
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Health - Feel better, live better
> http://health.yahoo.com