从 formatCurrency 中删除货币符号
Removing currency symbol from formatCurrency
我正在尝试格式化从数据库中获取的收入总额,并使用 php 的 NumberFormatter class 和 formatCurrency 方法。
但是,我不想用它打印出实际的 € / 欧元符号。我只想要纯数字,带逗号和小数点。
示例;
1234.56 应格式化为 1,234.56
当前输出为 1,234.56 欧元。
我使用的代码:
$array['total_revenue'] = $this
->db
->query($sql)
->row_array()['SUM( booking_total )'];
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
谁能告诉我如何解决这个问题以删除欧元符号?
一个简单的正则表达式可以快速解决您的问题。尝试;
$actual = $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
$output = preg_replace( '/[^0-9,"."]/', '', $actual );
echo $output;
希望对您有所帮助
您应该使用 setSymbol()
函数:
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
$formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
echo $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
我来这里是因为出于某种原因,$formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
被 $formatter->formatCurrency($array['total_revenue'], 'USD');
忽略了
为了解决这个问题,我在这里找到了解决方案。
https://www.php.net/manual/en/numberformatter.setsymbol.php#124153
this could be obvious to some, but setSymbol(NumberFormatter::CURRENCY_SYMBOL, '')
doesn't work for formatCurrency
- it will simply be ignored...
use NumberFormatter::CURRENCY
and $fmt->format(123);
to get a currency value with the symbol specified as CURRENCY_SYMBOL
(or INTL_CURRENCY_SYMBOL
)
即
$fmt = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
$fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
echo $fmt->format(56868993064.7985);
//Output: 56.868.993.064,80
我正在尝试格式化从数据库中获取的收入总额,并使用 php 的 NumberFormatter class 和 formatCurrency 方法。
但是,我不想用它打印出实际的 € / 欧元符号。我只想要纯数字,带逗号和小数点。
示例; 1234.56 应格式化为 1,234.56 当前输出为 1,234.56 欧元。
我使用的代码:
$array['total_revenue'] = $this
->db
->query($sql)
->row_array()['SUM( booking_total )'];
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
谁能告诉我如何解决这个问题以删除欧元符号?
一个简单的正则表达式可以快速解决您的问题。尝试;
$actual = $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
$output = preg_replace( '/[^0-9,"."]/', '', $actual );
echo $output;
希望对您有所帮助
您应该使用 setSymbol()
函数:
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
$formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
echo $formatter->formatCurrency($array['total_revenue'], 'EUR') . PHP_EOL;
我来这里是因为出于某种原因,$formatter->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
被 $formatter->formatCurrency($array['total_revenue'], 'USD');
为了解决这个问题,我在这里找到了解决方案。 https://www.php.net/manual/en/numberformatter.setsymbol.php#124153
this could be obvious to some, but
setSymbol(NumberFormatter::CURRENCY_SYMBOL, '')
doesn't work forformatCurrency
- it will simply be ignored...use
NumberFormatter::CURRENCY
and$fmt->format(123);
to get a currency value with the symbol specified asCURRENCY_SYMBOL
(orINTL_CURRENCY_SYMBOL
)
即
$fmt = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
$fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
echo $fmt->format(56868993064.7985);
//Output: 56.868.993.064,80