In AnQiCMS template development, formatting numbers for display, especially controlling decimal places, is a very common requirement.Whether you want to display the price of goods, the percentage in statistical data, or other numbers that need to be presented accurately, AnQiCMS's powerful template engine provides flexible filters to help you achieve these goals.

AnQiCMS template syntax is similar to Django, variables are usually output through double curly braces{{ 变量 }}and filters are output through the pipe symbol|Applied after the variable. Below, we will discuss in detail how to format numbers in the AnQiCMS template, especially retaining decimal places.

UsefloatformatFilter retains decimal places

When we need to control numbers, especially decimal numbers, accurately,floatformatThe filter is the preferred tool. This filter is specifically used for formatting floating-point numbers and provides various ways to retain decimal places.

The basic usage is{{ 变量|floatformat:参数 }}. Among them,参数It can be an integer used to specify the number of decimal places you want to keep.

  1. The default behavior (keep one decimal place, do not display trailing zeros)If no parameters are specified,floatformatThe default is to try to keep one decimal place. If the number itself is an integer, the decimal part will not be displayed; if the decimal part is only one digit and zero, it will also be omitted. For example,{{ 34.23234|floatformat }}It will display34.2while{{ 34.00000|floatformat }}it will display:34.

  2. Keep the specified number of decimal places (positive integer parameter)When you need to control the number of decimal places accurately, you can pass a positive integer as a parameter. For example, to keep three decimal places, you can use{{ 34.23234|floatformat:3 }}It will display34.232. Even if the decimal places are insufficient, it will be filled with zeros, such as{{ 34.00000|floatformat:3 }}Will be displayed34.000.

  3. Rounded to an integer (parameter is0or an empty string"")If you want to round a floating-point number to the nearest integer, you can pass0Or an empty string as a parameter. For example,{{ 39.56000|floatformat:"0" }}Will be displayed40.

  4. Negative integer parameters are calculated from right to left to determine the number of decimal places. floatformatNegative integer parameters are also supported, but this is less commonly used when retaining decimal places. It rounds from the end of the number. For example,{{ 34.23234|floatformat:"-3" }}It will still be displayed34.232Because it retains 3 decimal places, counting from the end, it just reaches the third place.

Through these flexible parameters,floatformatThe filter can meet the vast majority of needs for retaining decimal places.

UsestringformatFilter for more general number formatting

If your formatting requirements are more complex, or you are accustomed to formatting strings similar to the C language, thenstringformatThe filter will surprise you. It can format numbers, strings, and any value according to the format string you define.

Regarding retaining decimal places,stringformatusually combined%.nfThis format symbol is used, among whichnrepresents the number of decimal places you want to keep. For example, to keep two decimal places, you can use{{ 变量|stringformat:"%.2f" }}. If your variableitem.PriceThe value is3.141592653then{{ item.Price|stringformat:"%.2f" }}Will be displayed3.14.

stringformatThe strength lies in its versatility, it is not limited to floating-point numbers, but can also be used for integers (%d), strings (%s)and other complex formatting requirements, such as adding specific text or symbols before and after numbers. For example,{{ 888|stringformat:"Test: %d" }}It will displayTest: 888.

Auxiliary filter:integerandfloatEnsure data type

In some cases, the data you receive from the backend may not be strictly numeric types, such as prices or quantities stored as strings.Before formatting these values, it is best to convert them to actual numeric types to avoid potential errors or unexpected behavior.

integerA filter can convert a value to an integer. If the conversion fails, it will return0. For example,{{ "5.4"|float|integer }}Will be displayed5.

floatA filter can also convert a value to a floating-point number. If the conversion fails, it will return0.0. For example,{{ "foobar"|float }}Will be displayed0.000000.

In applicationfloatformatorstringformatBefore, use firstfloatThe filter ensures that you are processing a floating-point number, thereby ensuring the accuracy of the formatted results.

Summary

AnQiCMS template engine throughfloatformatandstringformatThese powerful filters provide flexible and precise control for the formatting display of numbers, especially for retaining decimal places.floatformatSimple and intuitive, suitable for directly controlling the number of decimal places; andstringformatIt provides C language style formatting capabilities, suitable for more complex output needs. CombinedintegerandfloatA helper filter that ensures proper data processing and perfect presentation, thereby greatly enhancing the professionalism and user experience of the AnQiCMS website.


Frequently Asked Questions (FAQ)

Q1: How can I add a currency symbol before or after a number?

A1: You can directly concatenate the currency symbol with the formatted number in the template. For example, ifitem.PriceIs the product price, and you want to keep two decimal places and display the Chinese Yuan symbol:{{ '¥' }}{{ item.Price|floatformat:2 }}Or use:stringformatMore concise:{{ item.Price|stringformat:"¥%.2f" }}.

Q2: Besides retaining decimal places, what other formatting operations can AnQiCMS templates perform on numbers?

A2: AnQiCMS template supports various number formatting operations. In addition tostringformatImplement various C language style formatting (such as zero-padding, specifying width, etc.), you can also useaddfilters to add numbers, ordivisiblebyFilter to determine if a number can be evenly divided by another number, etc. For more detailed number-related filters, you can refer totag-calc.mdandfilter-*.mda series of documents.

Q3: How to determine if a variable is a valid numeric type in a template?

A3: You can useintegerorfloatThe filter tries to convert the variable to a number and then make a judgment based on the conversion result. For example, ifsomeValueThis should be a number, but it may be text or null:{% set num_value = someValue|float %} {% if num_value != 0 or someValue == '0' %} <!-- 这是一个有效的数字(或者原始字符串就是'0') --> {% else %} <!-- 不是有效数字 --> {% endif %}This method can help you validate the data type before displaying, ensuring that the page will not crash due to invalid data.