61

I have some price values to display in my page.

I am writing a function which takes the float price and returns the formatted currency val with currency code too..

For example, fnPrice(1001.01) should print $ 1,000.01

1

8 Answers 8

150

The easiest answer is number_format().

echo "$ ".number_format($value, 2);

If you want your application to be able to work with multiple currencies and locale-aware formatting (1.000,00 for some of us Europeans for example), it becomes a bit more complex.

There is money_format() but it doesn't work on Windows and relies on setlocale(), which is rubbish in my opinion, because it requires the installation of (arbitrarily named) locale packages on server side.

If you want to seriously internationalize your application, consider using a full-blown internationalization library like Zend Framework's Zend_Locale and Zend_Currency.

3
  • 7
    For negative values you would get something like $-123.45, proper formatting should be -$123.45 however.
    – ZurabWeb
    Nov 26, 2014 at 18:26
  • @ZurabWeb did you find fix around this? Jun 17, 2020 at 1:03
  • 1
    @kimbarcelona $sign = "-"; $amount = "-500"; if(strpos($amount, $sign) !== false){ l.trim($amount, $sign); echo "-$ ".number_format($intval(amount), 2);}
    – Alvin Moyo
    Jun 23, 2020 at 4:04
70

with the intl extension in PHP 5.3+, you can use the NumberFormatter class:

$amount = '12345.67';

$formatter = new NumberFormatter('en_GB',  NumberFormatter::CURRENCY);
echo 'UK: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;

$formatter = new NumberFormatter('de_DE',  NumberFormatter::CURRENCY);
echo 'DE: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;

which prints :

 UK: €12,345.67
 DE: 12.345,67 €
1
  • 4
    So important to have a solution that doesn't require converting to a float first. So dangerous to use a float with currency. May 20, 2016 at 15:38
10

sprintf() is the PHP function for all sorts of string formatting http://php.net/manual/en/function.sprintf.php

I use this function:

function formatDollars($dollars){
  return '$ '.sprintf('%0.2f', $dollars);
}
2
  • 6
    this won't add commas (thousands separators)
    – bcosca
    Oct 25, 2010 at 11:38
  • 1
    Forgive the gravedig. You can wrap the $dollars (in the sprintf()) in a number_format(). That'll add the commas
    – Orsokuma
    Jan 3, 2014 at 5:33
7

I built this little function to automatically format anything into a nice currency format.

function formatDollars($dollars)
{
    return "$".number_format(sprintf('%0.2f', preg_replace("/[^0-9.]/", "", $dollars)),2);
}

Edit

It was pointed out that this does not show negative values. I broke it into two lines so it's easier to edit the formatting. Wrap it in parenthesis if it's a negative value:

function formatDollars($dollars)
{
    $formatted = "$" . number_format(sprintf('%0.2f', preg_replace("/[^0-9.]/", "", $dollars)), 2);
    return $dollars < 0 ? "({$formatted})" : "{$formatted}";
}
2
  • 1
    This doens't retain negative values May 18, 2016 at 13:42
  • This is true. I've updated the function to include negative number support. Thanks @FlyingNimbus
    – Jam
    Jun 24, 2016 at 15:52
4

Reference Link : https://www.php.net/manual/en/function.number-format.php

$amount = 1235.56
echo number_format($amount, 2, '.', ',');

The output is : 1,235.56

If you don't need comma in output.Please remove comma inside function.

For example

$amount = 1235.56
echo number_format($amount, 2, '.', '');

The output is : 1235.56

3

From the docs

<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>
2

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";

output will be

1.234.567,89 €
1.234.567,89 RUR

https://www.php.net/manual/en/numberformatter.formatcurrency.php

2
  • 2
    According to the PHP Docs: This function has been DEPRECATED as of PHP 7.4.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged. Dec 16, 2021 at 18:16
  • I have updated it. Thanks! Jan 20, 2022 at 20:01
1

PHP has a function called money_format for doing this. Read about this here.

2
  • 1
    this function money_format does not work on php version 7.1 Jan 16, 2019 at 9:54
  • this function is deprecated in 7.4 and removed from 8.0 May 17, 2022 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.