How do I format a number to a dollar amount in PHP


Question

How do you convert a number to a string showing dollars and cents?

eg:
123.45    => '$123.45'
123.456   => '$123.46'
123       => '$123.00'
.13       => '$0.13'
.1        => '$0.10'
0         => '$0.00'
1
55
1/31/2009 7:52:47 PM

Accepted Answer

PHP also has money_format().

Here's an example:

echo money_format('$%i', 3.4); // echos '$3.40'

This function actually has tons of options, go to the documentation I linked to to see them.

Note: money_format is undefined in Windows.

75
1/6/2011 2:32:47 AM

If you just want something simple:

'$' . number_format($money, 2);

number_format()


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon