如何在 PHP 中将数字格式化为美元金额

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/294865/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:16:23  来源:igfitidea点击:

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

phpformattingcurrency

提问by nickf

How do you convert a number to a string showing dollars and cents?

如何将数字转换为显示美元和美分的字符串?

eg:
123.45    => '3.45'
123.456   => '3.46'
123       => '3.00'
.13       => '
'$' . number_format($money, 2);
.13' .1 => '
echo money_format('$%i', 3.4); // echos '.40'
.10' 0 => '
    $number = "123.45";
    $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
    return $formatter->formatCurrency($number, 'USD');
.00'

回答by Darryl Hein

If you just want something simple:

如果你只是想要一些简单的东西:

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

number_format()

number_format()

回答by Jeremy Ruten

PHP also has money_format().

PHP 也有money_format()

Here's an example:

下面是一个例子:

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

This function actually has tons of options, go to the documentation I linked to to see them.

这个功能实际上有很多选项,去我链接的文档中查看它们。

Note: money_format is undefined in Windows.

注意:money_format 在 Windows 中是未定义的。



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

更新:通过 PHP 手册: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.

警告:从 PHP 7.4.0 开始,此函数 [ money_format] 已被弃用。强烈建议不要依赖此功能。

Instead, look into NumberFormatter::formatCurrency.

相反,请查看NumberFormatter::formatCurrency

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

回答by saadk

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.. :)

我试过了, money_format()但它根本不适合我。然后我尝试了以下一个。它对我来说很完美。希望它也能以正确的方式为您工作.. :)

you should use this one

你应该用这个

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

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

它将以货币格式显示货币编号,最多为 2 位小数。

回答by nickf

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

在 PHP 和 C++ 中,您可以使用 printf() 函数

#windows
extension=php_intl.dll

#linux
extension=php_intl.so

回答by Jon

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.

请注意,在 PHP 7.4 中,money_format() 函数已弃用。它可以被 intl NumberFormatter 功能取代,只要确保你启用了 php-intl 扩展。这是少量的努力并且值得,因为您可以获得很多可定制性。

$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);

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

Darryl Hein提到了仍然适用于 7.4 的快速方法:

/*     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)

回答by Frank Forte

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

在 php.ini 添加这个(如果没有的话):

##代码##

Then do this:

然后这样做:

##代码##

回答by asad saleem

##代码##