How to format a number to a dollar amount in PHP

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' 
+55
php formatting currency
Nov 17 '08 at 4:09
source share
5 answers

PHP also has money_format () .

Here is an example:

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

This function actually has many options, go to the documentation I am attached to to see them.

Note: money_format undefined on Windows.

+75
Nov 17 '08 at 4:13
source share

If you just need something simple:

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

number_format ()

+73
Nov 17 '08 at 4:18
source share

I tried money_format() , but that did not work for me at all. then I tried the following. It worked perfect for me. hope it will work correctly for you too .. :)

you should use this

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

it will display a monetary number in monetary format up to 2 decimal places.

+13
Feb 03 '14 at 1:07
source share

In PHP and C ++ you can use the printf () function

 printf("$%01.2f", $money); 
+6
Nov 17 '08 at 4:10
source share

In C #, use the currency format specifier: C

string.Format ("{0: C}", someValue);

0
Nov 17 '08 at 4:17
source share



All Articles