In the AnQi CMS, the display of content is the core of our daily work, and data, especially scenarios involving floating-point numbers such as finance and statistics, the display accuracy directly affects the professionalism and readability of the information.For example, product prices, statistical percentages, or various technical indicators can easily confuse users if the decimal places are not uniform or the display is not standardized.Auto CMS provides us with a convenient solution through its built-in template filters.

UtilizefloatformatFilter for floating-point number formatting

For most common floating-point formatting requirements, AnQi CMS provides a namedfloatformatThe filter, which is specifically used for handling the display precision of floating-point numbers. This filter is very intuitive and can retain the value of the variable to the specified decimal places in floating-point format.

When we do notfloatformat34.00000AfterfloatformatAfter processing, it will be displayed as34while34.23234will be displayed as34.2.

If we need to control the number of decimal places more accurately, for example, to display two or three decimal places, we canfloatformatThe filter will specify the required number of digits after the colon. For example, to format a floating-point number to three decimal places, we can use{{ value|floatformat:3 }}like this,34.23234will be displayed as34.232while34.00000will be displayed as34.000This method ensures that the decimal part is displayed with the specified number of digits in all cases, even if it is an integer, zeros are supplemented.

The following are somefloatformatHere are some examples of filter usage:

{# 默认行为:保留一位小数,末位为0则不保留 #}
{{ 34.23234|floatformat }}   {# 输出: 34.2 #}
{{ 34.00000|floatformat }}   {# 输出: 34 #}

{# 指定保留三位小数 #}
{{ 34.23234|floatformat:3 }} {# 输出: 34.232 #}
{{ 34.00000|floatformat:3 }} {# 输出: 34.000 #}

{# 即使是字符串形式的数字也能处理 #}
{{ "34.23234"|floatformat }} {# 输出: 34.2 #}
{{ "34.00000"|floatformat:3 }} {# 输出: 34.000 #}

adoptsstringformatFilter implementation for advanced formatting

Exceptfloatformat之外,English CMS还提供了功能更为强大的stringformat过滤器,它允许我们像Go语言中的fmt.Sprintf()Functions are the same, outputting numbers, strings, and other data through formatted strings. This provides great flexibility for scenarios that require more precise control over the format.

stringformatThe filter accepts a formatted string as a parameter, we can use Go languagefmtformatting verbs supported by the package. For floating-point numbers, the commonly used formatting verb is%.Nfwhere,NRepresents the number of decimal places to retain. For example,%.2fmeans to retain two decimal places,%.0fmeans to not retain decimals, and to display as an integer directly.

PassstringformatWe can achieve various complex floating-point number display effects, including but not limited to forcing the display of specific decimal places, zero-padding, and even combining with other text content for output.This is especially useful when dealing with numbers that require specific prefixes or suffixes, such as currencies, percentages, etc.

The following are somestringformatHere are some examples of filter usage:

{# 保留两位小数 #}
{{ 0.55555|stringformat:"%.2f" }} {# 输出: 0.56 (会进行四舍五入) #}

{# 保留零位小数(显示为整数) #}
{{ 123.45|stringformat:"%.0f" }} {# 输出: 123 (会进行四舍五入) #}

{# 结合文本输出,保留三位小数 #}
{{ 987.654321|stringformat:"当前数值为: %.3f" }} {# 输出: 当前数值为: 987.654 #}

{# 强制显示符号,并保留一位小数 #}
{{ -12.34|stringformat:"%+1.1f" }} {# 输出: -12.3 #}

How to choose the appropriate formatting method

In the AnQi CMS, when formatting the display of floating-point numbers, selectfloatformatOrstringformatmainly depends on your specific needs and requirements for control accuracy:

  • Choosefloatformat:when you only need to control the number of decimal places simply,floatformatThe default behavior (such as34.00000displayed as34) meets your expectations when specified,floatformatIt is a more concise and readable choice. It is suitable for most product prices, ratings, and other scenarios.
  • Choosestringformat:If you need more advanced formatting control, such as forcing the display of zeros after the decimal point, precise rounding behavior (both tend to round), zero-padding, or if you need to combine floating-point numbers with specific text for output, stringformatProvided unparalleled flexibility. It requires that you understand the formatting rules of the Go language.fmt.Sprintfbut can meet the most complex display requirements.

As website operators, we should choose the most appropriate formatting method according to the type of content and the target audience, ensuring that our website provides rich information while maintaining a professional and user-friendly presentation.


Common Questions and Answers (FAQ)

1. If my variable is not a floating-point number but an integer or a string, will these filters cause an error?

In most cases, these filters are designed to be robust. If your variable is an integer,floatformatandstringformatAlso handles it correctly and formats it as a floating-point number. If the variable looks like a number (for example,"123.45")floatformatAlso, it can be processed directly. However, if the variable is a string that cannot be parsed as a number at all (for example"hello"If so, it may output blank, zero values, or may cause template rendering errors in some strict modes.When in doubt about the data type, it is best to ensure that the variable content is a valid numeric representation before applying the filter.

2. In the SafeCMSfloatformatandstringformatThe filter performs decimal place processing, does it round up or round down?

According to common programming language conventions and Go languagefmt.Sprintfbehavior,floatformatandstringformatit is usually adopted when dealing with decimal placesRound offThe rule. For example,{{ 0.555|floatformat:2 }}It will output0.56In high-precision financial scenarios, in addition to front-end display, precise control at the back-end data processing level is also required to avoid potential errors.

3. Can I combine formatted floating-point numbers with other text or HTML tags for display?

Of course you can.These filters return formatted strings.You can directly embed these strings into your HTML structure, or combine them with other text content.<div>商品价格:¥{{ product.price|floatformat:2 }}元</div>Will combine the formatted price with “Product price: ¥” and “Yuan” for display. Forstringformat, you can even include text directly in the formatted string, such as{{ product.discount|stringformat:"优惠:%.1f%%" }}to display the percentage.