When building a website with AnQiCMS, we often encounter scenarios where we need to handle data type conversions, especially when data entered from the back-end is stored in string format but needs to be performed numerical calculations or comparisons on the front-end.For example, product price, quantity and other information, although it may be entered in the background in the form of a string such as "123.45", in the template, we may need to treat it as a number for arithmetic operations such as addition, subtraction, multiplication, or for conditional judgment based on the magnitude of the value.幸运的是,AnQiCMS强大的模板引擎提供了简便而高效的过滤器(filters),能够轻松实现这类数据转换,让您的内容运营和展示更加灵活。

Why do you need to convert a currency string to a numeric type?

Why do we need to make this conversion?Imagine that you are displaying a shopping cart page, where both the product prices and quantities are strings.If you want to calculate the total price of the product or display different promotional information based on price ranges, it cannot be done directly.

  • Perform mathematical operations:Easily perform addition, subtraction, multiplication, division, and other calculations.
  • Perform numeric comparisons:For example, judge whether the price is higher than a certain threshold.
  • Exact formatting:Although the displayed values can be filtered by other filters, the underlying data type is numeric, which can ensure the accuracy of the formatting.

English translation: AnQiCMS template solutions:floatandintegerFilter

AnQiCMS's template engine is based on Django template syntax, one of its core advantages is the rich set of 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 commonly used choice for handling monetary amounts because money often contains a decimal part.

    • Usage:{{ 您的字符串变量 | float }}
    • For example:{{ "123.45" | float }}it will output:123.45
    • If the conversion fails (for example, the string is "abc"), it will return by default0.0.
  • integerFilter:Used to convert a string to an integer (i.e., a number without a decimal point).If your data does not require a decimal part, or needs to perform integer operations after conversion, this filter will be very useful.

    • Usage:{{ 您的字符串变量 | integer }}
    • For example:{{ "123" | integer }}it will output:123
    • If the conversion fails, it will return by default0.

Actual application scenarios and examples.

Understood the basic usage of these filters, let's take a look at several specific application scenarios:

Scenario one: Convert product prices to numeric values

Assuming you have one in your content model:PriceField, 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 the product

If you need to calculate the quantity of a productQuantitywith pricePricethe product, remember, each string variable involved in the calculation needs to be converted to a numeric type first:

{% set totalPrice = archive.Price | float * archive.Quantity | float %}
<div>总价:{{ totalPrice }} 元</div>

Here we usedseta label to define a temporary variabletotalPriceStore the calculation results for later use. You can also perform calculations directly in the expression.

Scenario three: make conditional judgments based on numbers

Do you want to judge a promotional activity based on product price? Converting it to a number makes the conditional judgment easy:

{% if archive.Price | float > 100.00 %}
    <div>此商品享受满百减免优惠!</div>
{% else %}
    <div>再买一点就能享受优惠啦!</div>
{% endif %}

Scenario four: Display the number in a formatted manner after conversion

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. In this case, we can combinefloatformatIt uses a filter to implement. It receives a numeric parameter specifying the number of decimal places to retain.

<div>格式化后的价格:{{ archive.Price | float | floatformat:2 }} 元</div>

Please note,floatformatIt formats based on the numeric type.