Commerce
|
|
|
|
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
|
|
|
|