In the operation of e-commerce websites, the calculation and display of product prices are often one of the core links.AnQi CMS is an efficient content management system, although it mainly focuses on content management and presentation, its powerful template engine also supports us in performing some basic arithmetic operations, such as calculating the total sum of prices for two or more products, and ensuring that the final result is presented in the form of an accurate floating-point number.

This article will discuss in detail how to implement this requirement in the Anqi CMS template, 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 Anqi CMS template system, we usually usearchiveDetailTag to get detailed information of a single document, including the price of the product(Price) field.

Assuming that the document IDs corresponding to your two products are101and102. You can obtain their prices in the template in the following way:

{# 获取产品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 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 to ensure the accuracy of subsequent calculations in the template, it is best to explicitly convert it to a floating-point number type. This can be done by|floatFilter implementation.

{# 确保获取到的价格是浮点数类型,以便后续精确计算 #}
{% set price_a = productA_price_raw|float %}
{% set price_b = productB_price_raw|float %}

{% set ... %}Tags are used to define a variable and assign it in a template, which is very useful for storing intermediate calculation results.

Perform the summation operation of product prices.

The Anqi CMS template engine 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 point,total_sumThe variable will holdprice_aandprice_bThe result of the floating-point number after addition.

Ensure floating-point precision 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.3Although for most display scenarios, this error can be ignored, in sensitive scenarios involving money, we usually need to format the result to a specific number of decimal places (such as two) and round it to achieve a visual 'accuracy' effect.

The Anqi CMS template provides two powerful filters to help us format floating-point numbers:

  1. floatformatFilterThe filter is used to format floating-point numbers to a specified number of decimal places.By default, it will be formatted to one decimal place and omit the trailing zeros.By passing a numeric parameter, you can specify the number of decimal places to be retained. For example,|floatformat:2means retaining two decimal places.

    <p>产品总和(使用floatformat):<span style="color:red; font-weight:bold;">{{ total_sum|floatformat:2 }}</span> 元</p>
    
  2. stringformatFilterThis filter provides more flexible formatting capabilities, similar to the function in Go language.fmt.Sprintf()You can use%fformat specifiers to format floating-point numbers, and then proceed to%.2fSpecify to keep two decimal places.

    <p>产品总和(使用stringformat):<span style="color:green; font-weight:bold;">{{ total_sum|stringformat:"%.2f" }}</span> 元</p>
    

    Both methods can effectively control the display accuracy of floating-point number results, so that the total appears in the exact two decimal places we expect.

Complete operation example

Integrate the above steps and here is a complete code example that shows how to calculate the total sum of two product prices and display it accurately in the Anqi 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 Anqi CMS template.

Frequently Asked Questions (FAQ)

Q1: Why do simple floating-point additions sometimes result in something similar?0.1 + 0.2 = 0.30000000000000004What is the result?

Floating-point numbers are stored in binary form inside computers, which leads to some decimal fractions (such as 0.1, 0.2) being unable to be represented precisely, thus causing slight errors in calculations.The Go language used by AnQi CMS at the bottom layer also follows this general principle in floating-point arithmetic.Therefore, when performing floating-point addition and subtraction directly in the template, if an accurate decimal result is required, the most reliable way is to complete it on the backend (data layer)