How to accurately control the decimal places of floating-point numbers in AnQiCMS?

In website content operation, we often encounter situations where it is necessary to display prices, statistical data, measurement results, and other floating-point numbers.However, the original floating-point number often has unnecessary decimal places, which not only affects appearance but may also reduce the readability of the data.AnQiCMS fully considers this user's demand, providing flexible and powerful template filters to help us easily implement the formatting of floating-point numbers, making data display more professional and accurate.

applyfloatformatThe filter is used for decimal place control

AnQiCMS template language comes with a very useful tool, that isfloatformatA filter. Its main function is to round floating-point numbers to the specified decimal places according to our needs.

When we only need to perform routine processing on floating-point numbers, notfloatformatThe filter retains one decimal place by default when any parameter is specified. But there is a small detail to note: if the last digit of the decimal part is 0,floatformatThe decimal part will be automatically omitted to make the number look cleaner. For example, when processing34.23then, afterfloatformatfilter processing will display as34.2; while if processing34.00it will display as34.

If we need to control the decimal places more accurately, just infloatformatAdd a colon and the number you want to keep after the filter. For example, to format a floating-point number to retain two decimal places, you can write it in the template like this:{{ 34.23234|floatformat:2 }}. This will be the output34.23Similarly, if we want to keep three decimal places, we just need to change the parameter to3, that is{{ 34.23234|floatformat:3 }}The output will be34.232.

It is worth mentioning,floatformatEven supports negative numbers as parameters. For example, when the parameter is negative,floatformat:-3It rounds off the decimal points of floating-point numbers by counting digits from the end.This feature may be used when processing certain specific financial or scientific data, but for daily decimal place control, we usually use positive parameters.

Let us experience through some examplesfloatformatThe effect:

Suppose we have a floating-point numberproductPrice = 34.23234and `taxRate = 34.0