This article will discuss in detail how to implement this requirement in the template of AnQi CMS, covering how to obtain product prices, perform addition operations, and how to control floating-point precision.
Get product price information
Firstly, we need to obtain the price from the product document. In the template system of Anqi CMS, we usually usearchiveDetailLabel to get detailed information of a single document, including the price of the product (Price) field.
Assuming the document IDs corresponding to your two products are101and102。You can obtain their prices in the following ways in the template:
{# 获取产品A(ID为101)的价格 #}
{% archiveDetail productA_price_raw with name="Price" id="101" %}
{# 获取产品B(ID为102)的价格 #}
{% archiveDetail productB_price_raw with name="Price" id="102" %}
Here,productA_price_rawandproductB_price_rawThe original price data of the two products will be stored separately. It is worth noting that,PriceThe field is usually recognized as a numeric type when entered in the background, but in the template to ensure the accuracy of subsequent calculations, it is best to explicitly convert it to a floating-point number type. This can be done by|floatthe filter.
{# 确保获取到的价格是浮点数类型,以便后续精确计算 #}
{% set price_a = productA_price_raw|float %}
{% set price_b = productB_price_raw|float %}
{% set ... %}Tags are used to define a variable and assign a value in the template, which is very useful for storing intermediate calculation results.
Perform the sum of product prices calculation
The template engine of AnQi CMS supports basic arithmetic operations. Once you store the product price in a variable and ensure it is of floating-point type, you can use+The operator performs a simple addition.
{# 计算两个产品价格的总和 #}
{% set total_sum = price_a + price_b %}
At this time,total_sumThe variable will hold.price_aandprice_bThe result of the addition of floating-point numbers.
Ensure the precision of floating-point numbers and formatted output.
In computer science, floating-point arithmetic can sometimes produce tiny precision errors due to its internal binary representation (for example0.1 + 0.2May not be exactly equal to0.3)。Although this error can be ignored for most display scenarios, in sensitive scenarios involving amounts, we usually need to format the result to a specific decimal place (such as two decimal places) and round it to achieve a visual 'accuracy' effect.
The Anqi CMS template provides two powerful filters to help us format floating-point numbers:
floatformatFilterThe filter is used to format floating-point numbers to the specified decimal places.By default, it will be formatted to one decimal place and the trailing zeros will be omitted.By passing a numeric argument, you can specify the number of decimal places to be retained.|floatformat:2indicating that two decimal places should be retained.<p>产品总和(使用floatformat):<span style="color:red; font-weight:bold;">{{ total_sum|floatformat:2 }}</span> 元</p>stringformatFilter: This filter provides more flexible formatting capabilities, similar to the Go language'sfmt.Sprintf()function. You can use%fformat specifiers to format floating-point numbers, and by%.2fSpecify to keep two decimal places.<p>产品总和(使用stringformat):<span style="color:green; font-weight:bold;">{{ total_sum|stringformat:"%.2f" }}</span> 元</p>Both of these methods can effectively control the display precision of floating-point numbers, allowing the total to be presented in the exact two decimal places we expect.
Complete operation example
Combine the above steps, the following is a complete code example showing how to calculate the total sum of two product prices and display it accurately in the APT CMS template.
{# 假设有两个产品,ID分别为101和102 #}
{# 1. 获取产品A(ID为101)的原始价格 #}
{% archiveDetail productA_price_raw with name="Price" id="101" %}
{# 2. 获取产品B(ID为102)的原始价格 #}
{% archiveDetail productB_price_raw with name="Price" id="102" %}
{# 3. 确保获取到的价格是浮点数类型,并赋给新变量以便后续计算 #}
{# 假设产品A价格为 99.99,产品B价格为 149.50 #}
{% set price_a = productA_price_raw|float %}
{% set price_b = productB_price_raw|float %}
{# 4. 进行价格求和运算 #}
{% set total_sum = price_a + price_b %}
{# 5. 使用 floatformat 过滤器将总和格式化为两位小数 #}
<p>产品总和(使用floatformat):<span style="color:red; font-weight:bold;">{{ total_sum|floatformat:2 }}</span> 元</p>
{# 6. 也可以使用 stringformat 过滤器进行更精细的格式化 #}
<p>产品总和(使用stringformat):<span style="color:green; font-weight:bold;">{{ total_sum|stringformat:"%.2f" }}</span> 元</p>
{# 如果需要显示原始价格,也可以进行格式化 #}
<p>产品A价格: {{ price_a|floatformat:2 }} 元</p>
<p>产品B价格: {{ price_b|floatformat:2 }} 元</p>
By following these steps, you will be able to achieve precise summation and formatted display of product prices in the template of Anqi CMS.
Common Questions and Answers (FAQ)
Q1: Why does simple floating-point addition sometimes result in0.1 + 0.2 = 0.30000000000000004?