Keep floating-point numbers to a certain number of decimal places

How to output a floating-point number retaining 2 decimal places in Anqi CMS template?

floatformatThe filter can output a floating-point number with 2 decimal places.You can also retain the specified number of decimal places. For example, retain 3 decimal places or so.It supports negative number digits, if the number set is negative, it calculates from the last digit.

Usage method

floatformatHow to use the filter:

{{ obj|floatformat:number }}

For example, formatting is needed34.23234Keep 3 decimal places, it can be written like this:

{{ 34.23234|floatformat:3 }}
# 显示结果
34.232

Example Demonstration

{{ 34.23234|floatformat }}
{{ 34.00000|floatformat }}
{{ 34.26000|floatformat }}
{{ "34.23234"|floatformat }}
{{ "34.00000"|floatformat }}
{{ "34.26000"|floatformat }}
{{ 34.23234|floatformat:3 }}
{{ 34.00000|floatformat:3 }}
{{ 34.26000|floatformat:3 }}
{{ 34.23234|floatformat:"0" }}
{{ 34.00000|floatformat:"0" }}
{{ 39.56000|floatformat:"0" }}
{{ 34.23234|floatformat:"-3" }}
{{ 34.00000|floatformat:"-3" }}
{{ 34.26000|floatformat:"-3" }}
# 显示结果
34.2
34
34.3
34.2
34
34.3
34.232
34.000
34.260
34
34
40
34.232
34
34.260