In website content management, we often need to display various numbers, especially floating-point numbers with decimal points.Whether it is product prices, statistics, or calculation results, the way these numbers are presented directly affects user experience and the accuracy of information.Decimal points that are not uniform or precise may cause the page to appear disordered, and even lead to misunderstandings when involving finance or precise measurement.
English CMS (EnglishCMS) knows this need and provides a powerful function in the template system,floatformatFilter, designed to help content operators easily and accurately control the display of decimal places of floating-point numbers.Through this filter, you can ensure that the numerical information on the website is presented in a professional and easy-to-understand manner.
What isfloatformatFilter?
floatformatThe filter is a tool specifically used in the AnQi CMS template engine to handle floating-point number display.Its core function is to round a floating-point number according to the rules you set and control the number of decimal places.This includes default smart display, fixed-digit display, and even the implementation of rounding control for integer places.
Why do we needfloatformat?
Imagine that you are running an e-commerce website, where product prices might vary from9.90000to100.5Not equal.If the original data is displayed directly, the number of decimal places seen by the user will be uneven, and the experience will be very poor.For example, when displaying the bounce rate or conversion rate of website visitors, it is usually sufficient to be precise to one or two decimal places, as too many digits may distract the user's attention.floatformatFilters are exactly born to solve these problems, they can make your digital information:
- More organized and unified:Ensure that all numbers of the same type are displayed with the same decimal places.
- More professional and credible:Standardized number formats enhance the professionalism and trustworthiness of the website.
- Easier to read:Avoid redundant decimal places to allow users to catch the key information at a glance.
- Avoid ambiguity:Especially in financial and statistical scenarios, precise rounding rules are crucial.
floatformatHow to use the filter
floatformatThe basic usage syntax of the filter is very intuitive:
{{ 您的数字变量 | floatformat: [位数] }}
Among them:
您的数字变量Is the floating-point number (or numeric string) you want to format.[位数]Is an optional parameter used to specify the number of decimal places you want to retain.
Next, we will understand its different usages through specific scenarios.
1. No parameters used (default behavior)
When you don'tfloatformatFilter provides any parameter, it will adopt the default intelligent display strategy: English.
- Round off the floating point number to one decimal place. English.
- If the decimal point after the result is English.
0It will directly omit the decimal part and display as an integer.
{{ 34.23234|floatformat }}
{{ 34.00000|floatformat }}
{{ 34.26000|floatformat }}
{{ "34.23234"|floatformat }}
{{ "34.00000"|floatformat }}
{{ "34.26000"|floatformat }}
The corresponding display result will be:
34.2
34
34.3
34.2
34
34.3
As you can see,34.00000It will be displayed directly after processing.34while34.26000It will be rounded to34.3.
2. Specify a positive number parameter (fixed decimal places)
When you setfloatformatAn integer parameter for the filter (for example)2or3It will round off the floating-point number to the specified number of decimal places and ensure that it always displays this many digits, filling with zeros if it is not enough.
{{ 34.23234|floatformat:3 }}
{{ 34.00000|floatformat:3 }}
{{ 34.26000|floatformat:3 }}
{{ "34.23234"|floatformat:3 }}
{{ "34.00000"|floatformat:3 }}
{{ "34.26000"|floatformat:3 }}
The corresponding display result will be:
34.232
34.000
34.260
34.232
34.000
34.260
Here, even if the original value34.00000auto. The decimal part is all zeros, specified3After the specified decimal place, it will also be filled with34.000.
3. Specify the parameter0(Rounded to the nearest integer)
When you explicitly set the parameter to0whenfloatformatThe filter will round off floating-point numbers to the nearest integer without retaining any decimal places.
{{ 34.23234|floatformat:"0" }}
{{ 34.00000|floatformat:"0" }}
{{ 39.56000|floatformat:"0" }}
The corresponding display result will be:
34
34
40
This is very useful for scenarios that require displaying whole numbers, such as product quantities, ticket numbers, etc.
4. Specify negative number parameters (smart retention of decimal places)
Although the document mentions that negative parameters are 'calculated from the last digit', looking at the example results, its behavior seems more like rounding floating-point numbers to the specified positive number of decimal places and then intelligently removing the trailing zeros.