In AnQiCMS, the flexibility of content display is one of its core advantages.When dealing with floating-point numbers that require precise display of decimal places, understanding how to use its powerful template engine features becomes particularly important.Ensure that numbers are presented in the expected format, whether it's displaying product prices, statistics, or technical parameters, which can greatly enhance user experience and the professionalism of the website.

The template syntax of AnQi CMS is borrowed from the Django template engine, providing rich filters (Filters) for processing and formatting data. For the formatting display of floating-point numbers, we mainly use two very practical filters:floatformatandstringformat。They each have their own focus and can meet different precision control requirements.

1. UsefloatformatThe filter can be used for simple floating-point number formatting.

floatformatThe filter is a convenient tool specially designed for floating-point numbers in AnQiCMS templates. Its main feature is the ability to intelligently handle the display of numbers, especially the trailing zeros.

Basic usage:When you don'tfloatformatThe filter will perform the following intelligent processing when any parameter is specified:

  • If the decimal part of a floating-point number is.000..., it will be displayed as an integer directly. For example,34.00000it will be displayed as34.
  • If the decimal part contains non-zero digits, it will default to retaining one decimal place and rounding it. For example,34.23234it will be displayed as34.2while34.26000will be displayed as34.3.

Example code:Suppose we have a variableprice = 34.23234andstock = 34.00000.

<p>商品价格:{{ price|floatformat }}</p> {# 输出: 34.2 #}
<p>库存数量:{{ stock|floatformat }}</p> {# 输出: 34 #}

Precisely control the number of decimal places:If you need to control the number of decimal places accurately,floatformatProvide a positive integer parameter. This parameter will force the number to retain a specified number of decimal places, and insufficient ones will be filled with zeros.

Example code:We hope to convertpriceto retain three decimal places.

<p>精确价格:{{ price|floatformat:3 }}</p> {# 输出: 34.232 #}
<p>精确库存:{{ stock|floatformat:3 }}</p> {# 输出: 34.000 #}

The妙用 of negative number parameters: floatformatIt supports negative numbers as parameters, which provides a special way of rounding from right to left:

  • floatformat:-1: Similar to the default behavior, but does not retain trailing zeros.
  • floatformat:-2:Round the number to the nearest ten. For example,345.67it will be displayed as350.
  • floatformat:-3:Round the number to the nearest hundred. For example,345.67it will be displayed as300.

Example code:

<p>默认处理:{{ 34.23234|floatformat:-1 }}</p> {# 输出: 34.2 #}
<p>四舍五入到十位:{{ 345.67|floatformat:-2 }}</p> {# 输出: 350 #}
<p>四舍五入到百位:{{ 1234.56|floatformat:-3 }}</p> {# 输出: 1200 #}

2. UsestringformatThe filter implements more flexible formatting

stringformatThe filter is a more general data formatting tool, similar to that in many programming languagesprintforsprintfThe function. Although it is not only used for floating-point numbers, we can take advantage of its powerful placeholder formatting to precisely control the display of floating-point numbers.

Basic usage: stringformatAccept a format string as a parameter, which includes%placeholder at the beginning. For floating-point numbers, we usually use%.xfsuch a format, wherexis the number of decimal places you want to keep.

Example code:We want to translateprice = 34.23234to keep two decimal places.

<p>精确价格:{{ price|stringformat:"%.2f" }}</p> {# 输出: 34.23 #}
<p>填充价格:{{ price|stringformat:"%06.2f" }}</p> {# 输出: 034.23 (总宽度为6,不足用0填充,保留2位小数) #}

It is worth noting that,stringformatthe format is more strict, it will output strictly according to the format you specify, including zero padding, unlikefloatformatHow to omit trailing zeros in such a "intelligent" way.

3. How to choose an appropriate floating-point formatting tool?

  • Choosefloatformat:

    • When you need to quickly and intelligently format floating-point numbers, especially when you want the number to be displayed as an integer without a decimal, or when you need a simple rounding rule.
    • Its negative parameter provides a unique rounding function by bit (tens, hundreds).
    • Suitable for most common display scenarios of product prices, ratings, etc.
  • Choosestringformat:

    • When you need to control the format of numbers more precisely, for example, to force fixed width, left alignment/right alignment, zero-padding, etc., similar toprintfadvanced formatting requirements.
    • If you need to combine floating-point numbers with other text or numbers in a specific pattern for output,stringformatit also becomes more flexible.
    • Suitable for reports, specific data display, or when strict formatting standards must be followed.

By making reasonable use offloatformatandstringformatThese filters, you can easily implement the precise formatting display of floating-point numbers in the AnQiCMS template, making your website content more professional and readable.


Common Questions (FAQ)

  1. 问:为什么我使用{{ price|floatformat }}like10.00Such a price will be displayed as10,instead of10.00?Answer: This isfloatformatThe default intelligent processing behavior of the filter.When used without parameters, if the decimal part of a floating-point number is all zeros, it will simplify it to an integer form to keep it concise.10.00), Please use{{ price|floatformat:2 }}or{{ price|stringformat:"%.2f" }}.

  2. Q:floatformatWhat rule does the filter follow when rounding?Answer:floatformatThe filter typically follows the "round to the nearest even" banker's rounding rule, especially when dealing with numbers that are exactly halfway (for example,.5)时。但在实际应用中,对于大多数非边缘值,它的行为与我们日常理解的四舍五入(如auto).5Carry forward is similar. If you need to be accurate to a specified decimal place, it is recommended to provide the parameter.

  3. 问:My data is of numeric type, but sometimes it is an integer and sometimes a float. How can I process it to unify the format?答:AnQiCMS的模板引擎通常会智能处理数据类型。您可以直接使用 EnglishfloatformatFilter, it will also format it after conversion if necessary, even if it is an integer. For example,{{ integer_value|floatformat:2 }}will display integers as floating-point numbers with two decimal places (such as10Changes to10.00)。If an invalid non-numeric string is encountered, it may result in an empty or incorrect display. At this point, it is necessary to ensure that the data passed in is a valid numeric type.