In website content operation, the precise display of numbers is often crucial, whether it is product prices, statistical data, or technical indicators, the display accuracy of decimal points directly affects the accuracy of information and the understanding of users.AnQiCMS provides a flexible template engine, allowing us to easily adjust the decimal precision of numbers in front-end templates, thereby meeting various complex display requirements.

The AnqiCMS template system is developed based on Go language, with a syntax style similar to Django, and it provides great convenience for us to handle data display through its powerful Filter function. When we need to control the decimal point accuracy of numbers, we mainly usefloatformatandstringformatThese two filters.

Precise control:floatformatFilter

floatformatThe filter is a convenient tool specifically designed for floating-point numbers (decimals), which can help us quickly adjust the number of decimal places displayed.This filter intelligently handles the rounding of numbers and automatically removes trailing zeros as needed, making the display more concise.

Basic Usage One: Auto-Optimize DisplayIf We Do NotfloatformatThe filter specifies any parameter, it will automatically keep one decimal place for floating-point numbers, and if the decimal part ends in zero, it will directly remove the decimal part and display it as an integer. For example,34.23234It will be displayed as34.2while34.00000It will be displayed as34.

{# 假设 item.Price 的值为 34.23234 #}
<p>商品价格:{{ item.Price|floatformat }}</p> {# 输出: 34.2 #}

{# 假设 item.DiscountRate 的值为 34.00000 #}
<p>折扣比例:{{ item.DiscountRate|floatformat }}</p> {# 输出: 34 #}

{# 假设 item.TaxRate 的值为 34.26000 #}
<p>税率:{{ item.TaxRate|floatformat }}</p> {# 输出: 34.3 #}

Basic usage two: Specify the number of decimal placesIf we need to control the number of decimal places accurately, we can add tofloatformatThe filter passes a numeric parameter. This number represents the number of decimal places to retain. For example,floatformat:3It means retaining three decimal places. It also rounds off accordingly.

{# 假设 item.Price 的值为 34.23234 #}
<p>商品价格:{{ item.Price|floatformat:3 }}</p> {# 输出: 34.232 #}

{# 假设 item.DiscountRate 的值为 34.00000 #}
<p>折扣比例:{{ item.DiscountRate|floatformat:3 }}</p> {# 输出: 34.000 #}

{# 假设 item.TaxRate 的值为 34.26000 #}
<p>税率:{{ item.TaxRate|floatformat:3 }}</p> {# 输出: 34.260 #}

It is noteworthy that when the number of decimal places is specified as zero, such asfloatformat:0it will be displayed directly as an integer and rounded. For example39.56000|floatformat:0It will display40.

Dynamically adjust the number of decimal places floatformatThe filter also supports dynamic parameter passing, which means we can adjust the number of decimal places based on different contexts or configurations.For example, you can set a global decimal place in the background and then pass it to the filter through a variable.

{# 假设在某个地方我们定义了 decimalPlaces = 2 #}
{% set decimalPlaces = 2 %}
<p>动态价格:{{ item.Price|floatformat:decimalPlaces }}</p> {# 输出: 34.23 #}

Advanced customization:stringformatFilter

exceptfloatformatother than,stringformatThe filter provides more general string formatting capabilities, similar to those in programming languages.printfA function that controls output through string formatting. It is also applicable for floating-point precision control, and it can provide more fine-grained control, such as forcing the display of a certain number of decimal places, even if the trailing zeros.

Use%.xfControl the precision of floating-point numbers.By%.xfSuch a formatting string, we can specify the number of decimal places to be retained for floating-point numbers. In whichxThat is the number of digits you want to retain.

{# 假设 item.Price 的值为 3.141592653 #}
<p>商品价格(精确到两位):{{ item.Price|stringformat:"%.2f" }}</p> {# 输出: 3.14 #}

{# 假设 item.DiscountRate 的值为 34.00000 #}
<p>折扣比例(强制三位小数):{{ item.DiscountRate|stringformat:"%.3f" }}</p> {# 输出: 34.000 #}

{# 假设 item.TaxRate 的值为 34.26000 #}
<p>税率(精确到零位小数):{{ item.TaxRate|stringformat:"%.0f" }}</p> {# 输出: 34 #}

withfloatformatdifferent,stringformatWhen specified to a certain precision, it will strictly display the format string, including the zeros at the end. For example,34.00000|stringformat:"%.3f"It will still output34.000instead of34This is very useful in some scenarios where alignment or unified display format is required.

How to choose the appropriate filter?

ChoosefloatformatandstringformatAs needed, you can decide:

  • Selectfloatformat: If you need a concise floating-point display, allow automatic removal of trailing zeros and standard rounding.It focuses more on the display optimization of floating-point numbers, the default behavior usually meets most scenarios.
  • SelectstringformatIf you need more fine-grained control, such as forcing the display of a fixed number of decimal places (even if zero), or embedding numbers into more complex string formats.Its flexibility is higher, but it requires a certain understanding of format strings.

In practical application, for example, when displaying product details such as price, we can use it like this:

{# 从文档详情中获取产品的价格,并保留两位小数 #}
{% archiveDetail productPrice with name="Price" %}
<p>产品售价:¥{{ productPrice|floatformat:2 }}</p>

{# 或者,如果需要强制显示两位小数 #}
<p>产品售价(严格格式):¥{{ productPrice|stringformat:"%.2f" }}</p>

By flexibly using these filters, we can freely control the display accuracy of numbers in the AnQiCMS template, presenting more professional and readable content to users.


Frequently Asked Questions (FAQ)

  1. Can I dynamically adjust the display accuracy based on different user permissions or page types?Of course you can. You can pass the decimal precision as a variable to the filter. For example, set a variable nameddecimal_precisionin the background or through programming logic, and then use it in the template.{{ some_value|floatformat:decimal_precision }}or{{ some_value|stringformat:"%." ~ decimal_precision ~ "f" }}. So, different users or pages can see the values displayed with different precision.

  2. If my data is not of the floating-point number type, can I use it directly?floatformatorstringformatWill it cause an error? floatformatThe filter usually expects to receive floating-point numbers. If your data is an integer, it usually works fine.If your data is a string (for example, “123.45”) when usingfloatformatorstringformatBefore that, it's best to go throughfloatThe filter converts it to a floating-point number, for example{{ "123.45"|float|floatformat:2 }}This avoids potential type conversion errors.

  3. floatformatandstringformatWhen adjusting precision, should it be rounded or truncated directly?These two filters follow the standard rounding rules. For example, if you format12.345as two decimal places, it will display as12.35; if you format12.344as two decimal places, it will display as12.34They will not be truncated directly, but will be judged according to the next digit.