Mega Code Archive

 
Categories / Php / Strings
 

Easy Display of Dollars

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>"); } Example : <html> <? $amount=1.2345; echo("the value $amount expressed in dollars is ".dispdollars($amount)."."); ?> </html>