In the template development of AnQi CMS,floatformatThe filter is undoubtedly an important tool for handling floating-point number display, it can help us standardize the display of numbers on the page.However, when encountering zero values or decimal points followed by all zeros, the default behavior may lead to inconsistent display formats, which is particularly prominent in scenarios requiring strict uniformity (such as financial data, product prices).

floatformatBasic usage of filters

First, let's take a look backfloatformatThe basic function of the filter. Its main role is to round a floating-point number to the specified decimal places and output it.This filter can accept an optional numeric parameter to control the number of decimal places retained.

WhenfloatformatWhen no numerical parameter is attached, it will perform some default 'smart' processing:

  • It will round off to the nearest decimal place (if there is a decimal part).
  • It will remove all trailing zeros after the decimal point.

For example:

  • {{ 34.23234|floatformat }}Will be displayed34.2
  • {{ 34.00000|floatformat }}Will be displayed34
  • {{ 0.00000|floatformat }}Will be displayed0

And when it is provided with a positive number parameter, it will format strictly according to this number of digits, even if there are zeros after the decimal point, it will fill them with zeros:

  • {{ 34.23234|floatformat:3 }}Will be displayed34.232
  • {{ 34.00000|floatformat:3 }}Will be displayed34.000
  • {{ 0.00000|floatformat:3 }}Will be displayed0.000

The root cause of the inconsistent display format of zero

As shown in the above examples, the root cause of the problem lies infloatformatThe filter will automatically remove the trailing zeros after the decimal point if the number of decimal places is not explicitly specified. This is for values like34.00000which will ultimately be displayed as34; while for values like0.00000the display will be as0.

This "smart" processing may be convenient in some cases, but it can cause confusion in displays that require fixed decimal places, such as all prices must be displayed in the format of "XX.XX". This default behavior is actually chaotic.We may expect0displayed as0.00,34displayed as34.00Instead of simple0or34.

Ensure consistent display format for zero values

EnsurefloatformatThe key to consistent display format when handling zero values isAlways clearly specify the number of decimal places you wish to retain. Once you provide a numeric parameter,floatformatit will be formatted strictly to this number of digits, even if there is a zero after the decimal point, which will be filled with zeros to ensure consistency in display.

Assuming we want all floating-point numbers to be rounded to two decimal places:

  • For numeric values0: Use.{{ 0|floatformat:2 }}It will be displayed as0.00.
  • For integers100: Use.{{ 100|floatformat:2 }}It will be displayed as100.00.
  • For floating-point numbers with trailing zeros34.000: Use.{{ 34.000|floatformat:2 }}It will be displayed as34.00.
  • For regular floating-point numbers12.5: Use.{{ 12.5|floatformat:2 }}It will be displayed as12.50.

This ensures that the final display format will be unified to two decimal places, regardless of whether the original value is zero, an integer, or a floating-point number with different decimal places.

Actual application example

We demonstrate the difference between two usage methods through an example of a product price display:

`twig {% set price1 = 99 %} {% set price2 = 12.5 %} {% set price3 = 0 %} {% set price4 = 7.000 %}

The default floatformat filter displays:

Product A price: {{ price1|floatformat }} Yuan (Actual display: 99)

Product B price: {{ price2|floatformat }} Yuan (Actual display: 12.5)

Product C price: {{ price3|floatformat }} Yuan (Actual display: 0)

Product D price: {{ price4|floatformat }} Yuan (Actual display: 7)

Specify two decimal places flo