number_format

The number_format filter formats numbers. It is a wrapper around PHP's number_format function:

number_format フィルターは、数値をフォーマットします。これは、PHP の number_format 関数のラッパーです。
1
{{ 200.35|number_format }}

You can control the number of decimal places, decimal point, and thousands separator using the additional arguments:

追加の引数を使用して、小数点以下の桁数、小数点、桁区切り記号を制御できます。
1
{{ 9800.333|number_format(2, '.', ',') }}

To format negative numbers or math calculation, wrap the previous statement with parentheses (needed because of Twig's precedence of operators):

負の数または数学計算をフォーマットするには、前のステートメントを括弧で囲みます (Twig の演算子の優先順位のために必要です)。
1
2
3
4
{{ -9800.333|number_format(2, '.', ',') }} {# outputs : -9 #}
{{ (-9800.333)|number_format(2, '.', ',') }} {# outputs : -9,800.33 #}
{{  1 + 0.2|number_format(2) }} {# outputs : 1.2 #}
{{ (1 + 0.2)|number_format(2) }} {# outputs : 1.20 #}

If no formatting options are provided then Twig will use the default formatting options of:

フォーマット オプションが指定されていない場合、Twig は次のデフォルトのフォーマット オプションを使用します。
  • 0 decimal places.
    小数点以下 0 桁。
  • . as the decimal point.
    .小数点として。
  • , as the thousands separator.
    、千単位の区切り記号として。

These defaults can be changed through the core extension:

これらのデフォルトは、コア拡張機能を使用して変更できます。
1
2
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setNumberFormat(3, '.', ',');

The defaults set for number_format can be over-ridden upon each call using the additional parameters.

number_format に設定されたデフォルトは、追加のパラメーターを使用して呼び出しごとにオーバーライドできます。

Arguments

  • decimal: The number of decimal points to display
    decimal: 表示する小数点の数
  • decimal_point: The character(s) to use for the decimal point
    decimal_point: 小数点に使用する文字
  • thousand_sep: The character(s) to use for the thousands separator
    千_sep: 千単位の区切り記号に使用する文字