I'm no regex guru, but I believe this should work. It would be simpler if the regex engine could operate from right to left, but I'm not aware of any such capability:
<?
$cost = "857643242.30";
$str = '$'.strrev(preg_replace ("/(\d\d\d)(?=\d)/", "\1,", strrev($cost)));
print "Cost: $str";
?>
This assumes that you have less than 2 digits to the right of the decimal, other wise you'll probably want to split the string, and use the same regex, one reversed, the other normal.
-John
Simon weiss wrote:
Hi
How do you turn 1823545 to $1,823,545
Thanks
Simon
Derek C wrote:
Here is a nice little function that will take your number, and round it to 2 decimals. I believe you can do this simply with round() in PHP4 but not in PHP3. either way, thsi will round your numbers to 2 decimals.
function dollar($amount)
{
$amount=doubleval($amount);
echo(sprintf("%.2f", $amount));
}
Example:
dollar("182.83924");
This will print:
182.84
Now to align all your money to the right just use align="right" in your table or cell.