【问题标题】:How to hide decimal part of the localizedcurrency filter in twig如何在树枝中隐藏本地化货币过滤器的小数部分
【发布时间】:2016-06-21 08:01:02
【问题描述】:

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

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

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

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

谢谢!

【问题讨论】:

  • 尝试使用 0 位小数的 number_format 过滤器
  • 但不会在其locale对应位置添加货币符号,例如:100$ / $100
  • 然后尝试圆形过滤器。

标签: php symfony twig intl


【解决方案1】:

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

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

【讨论】:

  • 看来可能是这个,今晚我测试一下,我会回复你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
相关资源
最近更新 更多