在 PHP 中打印货币数字格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4013950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print Currency Number Format in PHP
提问by KoolKabin
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..
我正在编写一个函数,它采用浮动价格并返回带有货币代码的格式化货币 val ..
For example, fnPrice(1001.01)
should print $ 1,000.01
例如,fnPrice(1001.01)
应该打印$ 1,000.01
回答by Pekka
The easiest answer is number_format()
.
最简单的答案是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.
如果您希望您的应用程序能够使用多种货币和区域设置感知格式(1.000,00
例如,对于我们欧洲人来说),它会变得有点复杂。
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.
有money_format()
但它在 Windows 上不起作用并且依赖于setlocale()
,这在我看来是垃圾,因为它需要在服务器端安装(任意命名的)语言环境包。
If you want to seriously internationalize your application, consider using a full-blown internationalization library like Zend Framework's Zend_Localeand Zend_Currency.
如果您想认真地国际化您的应用程序,请考虑使用成熟的国际化库,如 Zend Framework 的Zend_Locale和Zend_Currency。
回答by Mathieu
with the intl extension in PHP 5.3+, you can use the NumberFormatterclass:
使用 PHP 5.3+ 中的 intl 扩展,您可以使用NumberFormatter类:
$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
回答by Jim
sprintf() is the PHP function for all sorts of string formatting http://php.net/manual/en/function.sprintf.php
sprintf() 是用于各种字符串格式的 PHP 函数 http://php.net/manual/en/function.sprintf.php
I use this function:
我使用这个功能:
function formatDollars($dollars){
return '$ '.sprintf('%0.2f', $dollars);
}
回答by DAWTSF
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}";
}
回答by sandeep kumar
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
?>