AnQiCMS's template system adopts the syntax of Django template engine, which means it provides a powerful set of filter (Filters) mechanisms.With these filters, we can perform various processing and formatting on the variable output.floatformat.
Core Tools:floatformatFilters
floatformatThe filter is specifically designed for floating-point numbers (i.e., numbers with decimals), which can flexibly control the display of decimal places and automatically perform rounding operations.
Basic Usage: Intelligent rounding and decimal omissionWhen you do not
floatformatThe filter will exhibit intelligent behavior when any parameter is specified.If the decimal part of a number is all zeros, it will only display the integer part without a decimal point; if the decimal part is not zero, it will be rounded to the nearest one decimal place.For example, suppose you have a variable
value:{{ value|floatformat }}- when
valueTo34.23234The output will be34.2. - when
valueTo34.00000The output will be34. - when
valueTo34.26000The output will be34.3.
This default behavior is very convenient in many scenarios, allowing numbers to be displayed both accurately and concisely.
- when
Specify the number of decimal places: control the display format accurately.If you need to control the number of decimal places more accurately, for example, always display two decimal places even if the decimal part of the number is zero, you can do so by
floatformatThe filter passes an integer parameter to implement. This parameter represents the number of decimal places you want to keep.For example, to format a number to keep three decimal places:
{{ value|floatformat:3 }}- when
valueTo34.23234The output will be34.232. - when
valueTo34.00000The output will be34.000. - when
valueTo34.26000The output will be34.260.
In this mode,
floatformatEnsure that the output number always has the specified number of decimal places, and if it is insufficient, it will be automatically filled with zeros.- when
Using negative number parameters: Advanced rounding requirements
floatformatThe filter also supports using negative numbers as parameters, which is very useful in certain specific financial or statistical scenarios. The meaning of a negative parameter is to round from the end of the number.