使用money_format:
setlocale(LC_MONETARY, 'en_US');
echo money_format('%.0i', $number);
'%.0i' 中的.0 是右侧精度。
您可能需要在 twig 扩展程序中添加过滤器。如果您需要帮助,请告诉我。
编辑:
查看 intl 扩展,它似乎从 NumberFormatter 类调用 formatCurrency 方法,该方法又调用私有 roundCurrency,最终这样做:
private function roundCurrency($value, $currency)
{
$fractionDigits = Intl::getCurrencyBundle()->getFractionDigits($currency);
// ...
// Round with the formatter rounding mode
$value = $this->round($value, $fractionDigits);
// ...
}
但是试图追踪 $fractionDigits 值的来源将在层次结构中再向上 2 个类,并变得像这样神秘:
public function getFractionDigits($currency)
{
try {
return $this->reader->readEntry($this->path, 'meta', array('Meta', $currency, static::INDEX_FRACTION_DIGITS));
} catch (MissingResourceException $e) {
return $this->reader->readEntry($this->path, 'meta', array('Meta', 'DEFAULT', static::INDEX_FRACTION_DIGITS));
}
}
因此可能可以让您当前的扩展来进行舍入,但我可能会使用我自己的过滤器,因为它似乎更容易并且我可以动态指定精度。