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

PhpFormattingCurrency

Php Problem Overview


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'

Php Solutions


Solution 1 - Php

If you just want something simple:

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

number_format()

Solution 2 - Php

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.


UPDATE: Via the PHP manual: https://www.php.net/manual/en/function.money-format.php

WARNING: This function [money_format] has been DEPRECATED as of PHP 7.4.0. Relying on this function is highly discouraged.

Instead, look into NumberFormatter::formatCurrency.

    $number = "123.45";
    $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
    return $formatter->formatCurrency($number, 'USD');

Solution 3 - Php

i tried money_format() but it didn't work for me at all. then i tried the following one. it worked perfect for me. hopefully it will work in right way for you too.. :)

you should use this one

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

it will show money number in terms of money format up to 2 decimal.

Solution 4 - Php

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

printf("$%01.2f", $money);

Solution 5 - Php

Note that in PHP 7.4, money_format() function is deprecated. It can be replaced by the intl NumberFormatter functionality, just make sure you enable the php-intl extension. It's a small amount of effort and worth it as you get a lot of customizability.

$f = new NumberFormatter("en", NumberFormatter::CURRENCY);
$f->formatCurrency(12345, "USD"); // Outputs "$12,345.00"

The fast way that will still work for 7.4 is as mentioned by Darryl Hein:

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

Solution 6 - Php

In php.ini add this (if it is missing):

#windows
extension=php_intl.dll

#linux
extension=php_intl.so

Then do this:

$amount = 123.456;

// for Canadian Dollars
$currency = 'CAD';

// for Canadian English
$locale = 'en_CA';

$fmt = new \NumberFormatter( $locale, \NumberFormatter::CURRENCY );
echo $fmt->formatCurrency($amount, $currency);

Solution 7 - Php

/*     Just Do the following, */

echo money_format("%(#10n","123.45"); //Output $ 123.45

/*    If Negative Number -123.45 */

echo money_format("%(#10n","-123.45"); //Output ($ 123.45)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickfView Question on Stackoverflow
Solution 1 - PhpDarryl HeinView Answer on Stackoverflow
Solution 2 - PhpPaige RutenView Answer on Stackoverflow
Solution 3 - PhpsaadkView Answer on Stackoverflow
Solution 4 - PhpnickfView Answer on Stackoverflow
Solution 5 - PhpJonView Answer on Stackoverflow
Solution 6 - PhpFrank ForteView Answer on Stackoverflow
Solution 7 - Phpasad saleemView Answer on Stackoverflow