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

The template system of AnQi CMS is developed based on Go language, with syntax style similar to Django. It provides great convenience for data display processing through powerful Filter features. When we need to control the decimal precision of numerical values, we will mainly usefloatformatandstringformatThese two filters.

Precise control:floatformatFilter

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

基本用法一:自动优化显示如果我们不为floatformatFilter specifies any parameter, it will automatically round off floating-point numbers to one decimal place, and if the decimal part ends with zero, it will directly remove the decimal part and display as an integer. For example,34.23234it will be displayed as34.2while34.00000will 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 Decimal PlacesIf we need to control the number of decimal places accurately,floatformatThe filter passes a numeric parameter. This number represents the number of decimal places to retain. For example,floatformat:3It represents retaining three decimal places. It will also round off.

{# 假设 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 worth noting that when the decimal places are specified as zero, such asfloatformat:0it will be displayed as an integer and rounded off directly. For example39.56000|floatformat:0will be displayed40.

Adjust the number of decimal places dynamically floatformatThe filter also supports dynamic parameter passing, which means we can adjust the number of decimal places according to 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

Exceptfloatformatin addition to,stringformatThe filter provides more general string formatting capabilities, similar to that in programming languages.printfFunction, used to control output through string formatting.For floating-point precision control, it also applies and can provide finer 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 numbersPass%.xfThis format string, we can specify the number of decimal places to retain for floating-point numbers. Whereinxis 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 at a certain precision, it will display strictly according to the format string, including the zeros at the end. For example,34.00000|stringformat:"%.3f"It will still be output34.000,instead of34. This is very useful in some scenarios where alignment or uniform display format is required.

How to choose an appropriate filter?

When choosingfloatformatandstringformatAt English, you can decide according to the actual needs:

  • ChoosefloatformatIf you need a concise floating-point number display, allow automatic removal of trailing zeros and standard rounding.It focuses more on the display optimization of floating-point numbers, and the default behavior often meets most scenarios.
  • ChoosestringformatIf you need more precise control, such as forcing the display of a fixed number of decimal places (even if it's zero), or need to embed the value into a more complex string format.Its flexibility is higher, but it requires a certain understanding of formatted strings.

In practical application, for example, when displaying the price in product details, 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 users with more professional and readable content.


Common Questions (FAQ)

  1. Can I dynamically adjust the display accuracy based on different user permissions or page types?Of course, you can pass the decimal precision as a variable to the filter. For example, you can 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 values displayed with different precision.

  2. If my data is not of floating-point type, can I just usefloatformatorstringformatit will cause an error? floatformatThe filter usually expects to receive floating-point numbers.If your data is an integer, it usually works fine.floatformatorstringformatBefore that, it's best tofloatFilter converts it to a floating-point number type, for example{{ "123.45"|float|floatformat:2 }}This can avoid potential type conversion errors.

  3. floatformatandstringformatWhen making precision adjustments, should rounding be used or truncation?These two filters follow the standard rounding rules. For example, if you format it as two decimal places, it will display as12.345Formatted as two decimal places, it will display as12.35;If formatted as two decimal places, it will display as12.344Formatted as two decimal places, it will display as12.34。They will not be truncated directly, but will be judged according to the next digit.