In AnQiCMS template development, formatting numbers for display, especially controlling the number of decimal places, is a very common requirement.No matter whether you want to display the price of the products, the percentage in the statistics, or other numbers that require precise presentation, AnQiCMS's powerful template engine provides flexible filters (filters) to help you achieve these goals.

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

UsefloatformatFilter retains decimal places

When we need to control numbers, especially decimal numbers, precisely,floatformatFilter is the preferred tool. This filter is specifically designed to format floating-point numbers and provides multiple ways to retain decimal places.

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

  1. Default behavior (retains one decimal place, does not display trailing zeros)If no parameter is specified,floatformatThe default is to try to retain 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 }}will be displayed34.2while{{ 34.00000|floatformat }}it will display:34.

  2. 保留指定小数位数(正整数参数)当您需要精确控制小数位数时,可以传递一个正整数作为参数。 例如,要保留三位小数,可以使用{{ 34.23234|floatformat:3 }}will display34.232Even if the decimal places are insufficient, it will be filled with zeros, for example{{ 34.00000|floatformat:3 }}Will be displayed34.000.

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

  4. Negative integer arguments (counting decimal places from right to left) floatformatIt still supports negative integer parameters, but this is rarely used in the need to retain 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 is exactly the third digit.

Through these flexible parameters,floatformatthe filter can meet the vast majority of decimal place retention needs.

UsestringformatFilter performs more general numeric formatting

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

Regarding the number of decimal places to retain,stringformatusually combined%.nfSuch a format symbol is used, wherenrepresents the number of decimal places you wish to retain. For example, to retain two decimal places, you can use{{ 变量|stringformat:"%.2f" }}. If your variableitem.Pricehas a value of3.141592653so that{{ item.Price|stringformat:"%.2f" }}Will be displayed3.14.

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

Auxiliary filter:integerandfloatEnsure data type

In some cases, the data you retrieve from the background may not be strictly of numeric type, such as prices or quantities stored in string form.Before formatting these values, it is best to convert them to actual numeric types to avoid potential errors or unexpected behavior.

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

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

In the applicationfloatformatorstringformatfirstfloatThe filter ensures that you are dealing with a floating-point number, thereby ensuring the accuracy of the formatted result.

Summary

AnQiCMS's template engine throughfloatformatandstringformatThese powerful filters provide flexible and precise control over the formatted display of numbers, especially in retaining decimal places.floatformatSimple and intuitive, suitable for direct control of decimal places;stringformatthen provided C language style formatting capabilities, suitable for more complex output needs. CombinedintegerandfloatThe auxiliary filter, which ensures the correct processing and perfect presentation of data, thus greatly enhancing the professionalism and user experience of the AnQiCMS website.


Common Questions (FAQ)

Q1: How do 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,item.PriceIt is the product price, and you want to keep two decimal places and display the RMB symbol:{{ '¥' }}{{ item.Price|floatformat:2 }}or usingstringformatMore 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 determines whether 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 judge if a variable is a valid number 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,someValueThis should be a number, but it might 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 it, ensuring that the page will not crash due to invalid data.