在 PHP 中打印货币数字格式

发布于 2024-09-29 16:14:15 字数 136 浏览 3 评论 0原文

我的页面中要显示一些价格值。

我正在编写一个函数,它接受浮动价格并返回带货币代码的格式化货币 val。

例如,fnPrice(1001.01) 应该打印 $ 1,000.01

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

谈情不如逗狗 2024-10-06 16:14:15

最简单的答案是 number_format()

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

如果您希望您的应用程序能够使用多种货币和区域设置感知格式(例如,对于我们一些欧洲人来说 1.000,00),它会变得有点复杂。

money_format() 但它在 Windows 上不起作用并且依赖于setlocale(),在我看来这是垃圾,因为它需要在服务器端安装(任意命名的)语言环境包。

如果您想认真地国际化您的应用程序,请考虑使用成熟的国际化库,例如 Zend Framework 的 Zend_LocaleZend_Currency

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.

凉城凉梦凉人心 2024-10-06 16:14:15

通过 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;

打印:

 UK: €12,345.67
 DE: 12.345,67 €

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 €
本宫微胖 2024-10-06 16:14:15

sprintf() 是用于各种字符串格式化的 PHP 函数
http://php.net/manual/en/function.sprintf.php

我使用这个函数:

function formatDollars($dollars){
  return '$ '.sprintf('%0.2f', $dollars);
}

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);
}
ˇ宁静的妩媚 2024-10-06 16:14:15

我构建了这个小函数来自动将任何内容格式化为良好的货币格式。

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

编辑

有人指出,这不显示负值。我将其分成两行,以便更容易编辑格式。如果它是负值,则将其括在括号中:

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

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}";
}
摇划花蜜的午后 2024-10-06 16:14:15

参考链接: https://www.php.net/manual/en /function.number-format.php

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

输出为:1,235.56

如果输出中不需要逗号。请删除函数内的逗号。

例如

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

输出为:1235.56

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

醉生梦死 2024-10-06 16:14:15

来自 文档

<?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

?>

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

?>
浪漫人生路 2024-10-06 16:14:15

(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";

输出将为

1.234.567,89 €
1.234.567,89 RUR

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

(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

烟酉 2024-10-06 16:14:15

PHP 有一个名为 money_format 的函数来执行此操作。请此处了解此内容。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文