如何在树枝中隐藏本地化货币过滤器的小数部分

How to hide decimal part of the localizedcurrency filter in twig

我正在使用过滤器 localizedcurrency 以多语言格式设置价格 网站。因为是房价,所以不需要小数部分

除了使用 replace 过滤器外,我找不到隐藏它的方法。我想知道是否有更好的方法,因为使用 replace 意味着我必须替换英文中的点和法文中的逗号等等...

我也不能使用某种形式的 substr,因为美元符号可以在值之前或之后。

我尝试将 int 而不是 float 传递给过滤器,但它只是默认为 .00

谢谢!

使用money_format:

    setlocale(LC_MONETARY, 'en_US');
    echo money_format('%.0i', $number);

'%.0i'中的.0为右侧精度

您可能需要为您的树枝扩展添加过滤器。如果您需要这方面的帮助,请告诉我。

编辑:

查看 intl 扩展,它似乎从 NumberFormatter class 调用 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 class,并变得像这样神秘:

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

因此 可能 可以让您当前的扩展名进行舍入,但我可能会使用我自己的过滤器,因为它似乎更容易,而且我可以指定精度即时。