As an experienced website operations expert, I know that every detail in content display can affect user experience and the accuracy of data.In the template world of AnQi CMS, we not only ensure the richness and fluency of content, but also strive for perfection in the presentation of data - especially floating-point numbers.floatformatFilter, it can help us precisely control the decimal places of floating-point numbers, making your website data display more professional and elegant.

Why do we need to refine the display of floating-point numbers?

In website operation, we often encounter scenarios where we need to display floating-point data such as prices, percentages, ratings, etc. If these data are not properly processed, the following situations may occur:

  • Redundant decimal places: 199.999999Such a price display is not only unattractive but may also confuse users.
  • Insufficient precision:When displaying prices with precision to two decimal places, if the system only shows integers, critical information will be lost.
  • Inconsistent format:Some data displays one decimal place, while some show two, lack of consistency will make the page look disorganized.

The AnQi CMS adopts a template engine syntax similar to Django, providing us with powerfulfloatformatFilter, it is to solve these problems, to ensure that floating-point numbers can be presented in the most appropriate and professional manner in various scenarios.

floatformatCore usage and practice of the filter

floatformatThe filter is very simple and intuitive to use, but its flexibility can meet the vast majority of display needs. We will peel back its mysterious veil through specific examples.

1. Default behavior: Smart and concise

When you don'tfloatformatThe filter exhibits intelligent default behavior when any parameter is provided.It will try to round the floating-point number to one decimal place.But what's even more thoughtful is that if the decimal part of the number is zero after rounding, it will directly remove the decimal point and the zeros following it, making the number look simpler.

Usage example:

{{ value|floatformat }}

Actual effect:

  • IfvalueYes34.23234to display34.2
  • IfvalueYes34.00000to display34
  • IfvalueYes34.26000to display34.3(Rounded off here)

This default behavior is very suitable for those who do not have high precision requirements but still want to keep the interface tidy, such as approximate prices in product lists.

2. Precise Decimal Place Control: Fixed Display N Digits

When you have a specific requirement for the number of decimal places displayed,floatformatFilter passes an integer parameter to specify the exact number of decimal placesNIt will always keep the one you specify at this pointNDecimal places, if the decimal places of the original number are insufficient, zeros will be automatically filled; if it exceeds, it will be rounded off.

Usage example:

{{ value|floatformat:N }}

For example, we want the price to always display two decimal places:

{{ product.price|floatformat:2 }}

Actual effect:

  • Ifproduct.priceIt is `34.232