How to accurately control the number of decimal places of a floating-point number in AnQi CMS?

In website content operation, we often encounter situations where we need to display floating-point numbers such as prices, statistics, and measurement results.However, the original floating-point number often has unnecessary decimal places, which not only affects the appearance but may also reduce the readability of the data.AnQiCMS fully considers this user's need and provides flexible and powerful template filters, which help us easily format floating-point numbers, making data display more professional and accurate.

ApplicationfloatformatFilter performs decimal place control

AnQiCMS built-in a very practical tool, namelyfloatformatFilter. Its main function is to round off floating-point numbers according to our needs and retain the specified number of decimal places.

When we only need to perform routine processing on floating-point numbers and do notfloatformatThe filter retains one decimal place by default when any parameter is specified. However, 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 simpler. For example, when processing34.23it will befloatformatdisplayed after filter processing as34.2; while if processing34.00it will be displayed as34.

If we need to control the number of decimal places more accurately, we just need to infloatformatAdd a colon followed by the expected number of digits to the filter. For example, to format a floating-point number to two decimal places, you can write it like this in the template:{{ 34.23234|floatformat:2 }}。At this moment, the output will be34.23。Similarly, if you want to keep three decimal places, you just need to change the parameter to3, which is{{ 34.23234|floatformat:3 }}The output will be34.232.

It is worth mentioning that,floatformatIt even supports negative numbers as parameters. For example, when the parameter is negative,floatformat:-3It will round to the specified number of decimal places from the end of the decimal point of the floating-point number.This function may be used when processing certain specific financial or scientific data, but for daily decimal place control, we usually use a positive parameter.

Let us experience intuitively through some examplesfloatformatThe effect is:

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