In the presentation of website content, the accuracy and readability of numbers are crucial for enhancing user experience and the effectiveness of information communication.Whether it is to display product prices, financial data, or various statistical indicators, we often need to round numbers or retain specific decimal places.As a template developer or content operator for AnQiCMS, understanding how to flexibly control the display format of numbers is a crucial step in achieving professional content presentation.

AnQiCMS's template system adopts the syntax of Django template engine, which means it provides a powerful filter (Filters) mechanism.We can apply various processing and formatting to the variable output through these filters.For rounding and decimal place control of numbers, AnQiCMS provides a very practical and powerful built-in filter:floatformat.

Core Tool:floatformatFilter

floatformatThe filter is designed specifically for floating-point numbers (i.e., numbers with decimals), it can flexibly control the number of decimal places displayed in the number, and automatically performs rounding operations.

  1. Basic usage: Intelligent rounding and decimal truncationWhen you do notfloatformatThe filter shows intelligent behavior when any parameters are 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 round to the nearest one decimal place.

    For example, assume you have a variablevalue:

    {{ value|floatformat }}
    
    • WhenvalueWith34.23234The output will be34.2.
    • WhenvalueWith34.00000The output will be34.
    • WhenvalueWith34.26000The output will be34.3.

    This default behavior is very convenient in many scenarios, allowing numbers to be displayed both accurately and concisely.

  2. Specify the number of decimal places: precise control of display formatIf 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 byfloatformatThe filter passes an integer parameter to be implemented. This parameter represents the number of decimal places you want to retain.

    For example, to format a number to retain three decimal places:

    {{ value|floatformat:3 }}
    
    • WhenvalueWith34.23234The output will be34.232.
    • WhenvalueWith34.00000The output will be34.000.
    • WhenvalueWith34.26000The output will be34.260.

    in this mode,floatformatEnsure that the output number always has the specified number of decimal places, and zeros will be automatically added if it is insufficient.

  3. Use negative parameters: advanced rounding requirements floatformatThe filter also supports using negative numbers as parameters, which is very useful in certain financial or statistical scenarios. The meaning of negative parameters is to round from the end of the number. For example