Zend - The PHP Company




Commerce

Add Code


Easy Display of Dollars?  

Type: code fragment
Added by: woodys
Entered: 13/09/2000
Last modified: 01/12/2000
Rating: ** (3 votes)
Views: 9887
You know those stupid little things you come accross after years in the profession, that make you say "Golly, if I'd just known that 3 or 4 years ago, it would have saved a ton of work!" This is one of them.


what is the easiest way to express a formatted dollar amount?

<? echo(sprintf("$ %.2f",$amount)); ?>

Here is a more robust function:

<? function dispdollars($amount$style)
{
   
//slam it!
   
$amount=doubleval($amount);

   if (
$amount>=0)
      echo(
sprintf("$ %.2f",$amount);
   else
      if (
$style==0)
         echo(
sprintf("-$ %.2f",-$amount);
      else
         echo(
"<font color=red>".sprintf("$ %.2f",-$amount)."</font>");

}





Usage Example


<html>
<?
   $amount
=1.2345;

   echo(
"the value $amount expressed in dollars is ".dispdollars($amount).".");

?>

</html>


Rate This Script





Search



This Category All Categories