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



我正在使用过滤器本地化货币以多种语言格式化价格网站。由于这是房屋的价格,因此我不需要小数部分。

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

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

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

谢谢!

使用money_format:

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

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

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

编辑:

查看 intl 扩展,它似乎从类中调用formatCurrency方法NumberFormatter而类又调用了私有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));
    }
}

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

最新更新