As an experienced CMS website operation person in the security industry, I know that both data accuracy and aesthetics are equally important in content display.Especially for floating-point numbers, how to format them to display the specified number of decimal places is a key link to improve user experience and ensure clarity of information.The powerful template engine of AnQi CMS provides flexible filter (filters) functions, allowing us to easily meet this requirement.

In Anqi CMS, displaying content is the core of our daily work, and data, especially in 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, if the decimal places are not uniform or displayed irregularly, it is easy to confuse the user.The Anqi CMS provides us with a convenient solution through its built-in template filters.

UtilizefloatformatFilter performs floating-point number formatting

For most common floating-point formatting needs, 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 in a floating-point format with a specified number of decimal places.

When we do notfloatformatThe filter retains one decimal place by default when any parameter is specified.However, it is worth noting that if the last digit of a floating-point number is zero, the filter will not retain the decimal point and will instead display the integer part. For example,34.00000afterfloatformatIt will be displayed after processing34while34.23234It will 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 canfloatformatAfter the filter, specify the required number of digits using the colon. For example, to format a floating-point number to retain three decimal places, we can use{{ value|floatformat:3 }}Thus,34.23234It will be displayed as34.232while34.00000It will be displayed as34.000This way ensures that the decimal part is displayed with the specified number of digits, even if it is an integer.

The following are somefloatformatExample of using a filter:

{# 默认行为:保留一位小数,末位为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 #}

usestringformatFilter achieves advanced formatting.

exceptfloatformatIn addition, Anq CMS also provides more powerful featuresstringformatThe filter allows us to use like in Go languagefmt.Sprintf()Functions are similar, they output numbers, strings, and other formats through string formatting. This provides great flexibility for scenarios that require more refined control of the format.

stringformatThe filter accepts a formatted string as a parameter, we can use Go languagefmtFormatting verbs supported in the package. For floating-point numbers, the commonly used formatting verb is%.Nfof whichNRepresents the number of decimal places to be retained. For example,%.2fmeans to retain two decimal places,%.0fthen it means to not retain the decimal, and display it as an integer directly.

BystringformatWe can achieve various complex floating-point number display effects, including but not limited to enforcing 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 somestringformatExample of using a filter:

{# 保留两位小数 #}
{{ 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

When formatting floating-point numbers in Anqi CMS, you can choosefloatformatOrstringformatIt mainly depends on your specific needs and requirements for controlling precision:

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

As website operators, we should choose the most suitable formatting method based on the type of content and target audience, ensuring that our website provides rich information while maintaining a professional and user-friendly display.


Frequently Asked Questions (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,floatformatandstringformatCan also be correctly processed and formatted as a floating-point number. If the variable looks like a numeric string (such as"123.45")floatformatIt can also be processed directly. However, if the variable is a string that cannot be parsed as a number (such as"hello"It may result in blank output, zero values, or template rendering errors in some strict modes.When in doubt of the data type, it is best to ensure that the variable content is a valid numerical representation before applying the filter.

2. In AnqiCMSfloatformatandstringformatIs the filter rounded up or rounded down when handling decimal places?

According to common programming language conventions and Go languagefmt.Sprintfbehavior,floatformatandstringformatWhen handling decimal places, it is usually adoptedRound offThe rule. For example,{{ 0.555|floatformat:2 }}It will output.0.56In financial scenarios with extremely high accuracy requirements, in addition to front-end display, precise control is also needed at the back-end data processing level to avoid potential errors.

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

Of course you can. These filters return formatted strings.You can directly embed these strings into your HTML structure or concatenate them with other text content. For example,<div>商品价格:¥{{ product.price|floatformat:2 }}元</div>The formatted price will be combined 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.