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

Oyl*_*lex 4 php intl symfony twig

我正在使用过滤器localizedcurrency在多语言网站中设置价格格式。由于这是房屋价格,因此我不需要小数部分。

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

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

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

谢谢!

Kar*_*lis 5

使用money_format

    setlocale(LC_MONETARY, 'en_US');
    echo money_format('%.0i', $number);
Run Code Online (Sandbox Code Playgroud)

.0'%.0i'是右侧的精度。

您可能需要在细枝扩展中添加过滤器。让我知道您是否需要帮助。

编辑:

研究intl扩展,似乎是formatCurrencyNumberFormatter类调用方法,而该类又调用了private roundCurrency,最终实现了:

private function roundCurrency($value, $currency)
{
    $fractionDigits = Intl::getCurrencyBundle()->getFractionDigits($currency);

    // ...

    // Round with the formatter rounding mode
    $value = $this->round($value, $fractionDigits);

    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是尝试追踪$fractionDigits价值的来源将在层次结构中再增加两个类,并且变得像这样神秘:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,它也许可以让您当前的分机做四舍五入,但我可能会用我自己的过滤器去,因为它似乎更容易,我可以在飞行中指定的精度。