When building a website with AnQiCMS, we often encounter scenarios where we need to handle data type conversions, especially when data from the backend is stored as a string but needs to be performed numerical calculations or comparisons on the frontend.For example, product price, quantity information, although it may be entered as a string such as "123.45" in the background, but in the template, we may need to treat it as a number for addition, subtraction, multiplication, or conditional judgment based on the size of the value.Fortunately, AnQiCMS's powerful template engine provides convenient and efficient filters (filters), which can easily achieve this kind of data conversion, making your content operation and display more flexible.
Why do you need to convert currency strings to numeric types?
Why do we need to perform this transformation? Imagine that you are displaying a shopping cart page, where both the product price and quantity are strings.If you want to calculate the total price of goods or display different promotional information based on price ranges, you cannot do so directly.After converting these strings to numeric types, you can:
- Perform mathematical operations:Easily perform addition, subtraction, multiplication, division, and other calculations.
- Perform numerical comparison:For example, check if the price is higher than a certain threshold.
- Precise formatting:Although the numeric display can be used with other filters, the underlying is numeric type, which can ensure the accuracy of formatting.
The solution in AnQiCMS template:floatandintegerFilter
AnQiCMS template engine is based on Django template syntax, one of its core advantages is the rich filter functions. For converting strings to numeric types, we mainly use two very practical filters:floatandinteger.
floatFilter:Used to convert a string to a floating-point number (i.e., a number with a decimal point). This is the most common choice for processing monetary amounts, as money often contains decimal parts.- Usage:
{{ 您的字符串变量 | float }} - For example:
{{ "123.45" | float }}will output123.45 - If the conversion fails (for example, if the string is "abc"), it will return by default
0.0.
- Usage:
integerFilter:Used to convert a string to an integer (i.e., a number without a decimal point).If your data does not require decimal parts or needs to be converted for integer operations, this filter is very useful.- Usage:
{{ 您的字符串变量 | integer }} - For example:
{{ "123" | integer }}will output123 - If the conversion fails, it will return by default
0.
- Usage:
Actual application scenarios and examples
After understanding the basic usage of these two filters, let's look at some specific application scenarios:
Scenario one: Convert product prices to numbers
Assuming there is one in your content modelPriceThe field stores the price in string format, such as{{ archive.Price }}. To convert it to a floating-point number in the template, just applyfloatFilter:
<div>商品价格:{{ archive.Price | float }} 元</div>
Scenario two: Calculate the total price of goods
If you need to calculate the quantity of a productQuantitywith the pricePriceof the product, remember that each string variable involved in the calculation needs to be converted to a numeric type:
{% set totalPrice = archive.Price | float * archive.Quantity | float %}
<div>总价:{{ totalPrice }} 元</div>
We used heresettag to define a temporary variabletotalPriceStore the calculation result for later use. You can also perform calculations directly in the expression.
Scenario 3: Conditional judgment based on numbers.
Do you want to judge the discount activities based on product prices? After converting to a number, the conditional judgment becomes effortless:
{% if archive.Price | float > 100.00 %}
<div>此商品享受满百减免优惠!</div>
{% else %}
<div>再买一点就能享受优惠啦!</div>
{% endif %}
Scenario four: Displaying the number after formatting
AlthoughfloatThe filter converts a string to a number, but sometimes we also need to control the display format of the number, such as retaining two decimal places. At this point, we can combinefloatformatTo implement a filter. It takes a numeric parameter specifying the number of decimal places to retain.
<div>格式化后的价格:{{ archive.Price | float | floatformat:2 }} 元</div>
Please note,floatformatIt is based on the numeric type for formatting,