In website content management, we often need to display various numbers, especially floating-point numbers with decimal points.The way numbers are presented directly affects user experience and the accuracy of information, whether it is product prices, statistical data, or calculation results.Inconsistent or imprecise decimal places may cause the page to appear chaotic, and may even lead to misunderstandings in financial or precise measurement situations.

AnQiCMS (AnQiCMS) understands this need and provides powerful functions in the template systemfloatformatFilter designed to help content operators easily and accurately control the display of decimal places for floating-point numbers.Through this filter, you can ensure that the numeric information on the website is presented in a professional and easy-to-understand manner.

What isfloatformatFilter?

floatformatThe filter is a tool in Anqi CMS template engine that is specifically used for handling floating-point number display.The 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 number of digits display, and even the control of rounding off integer digits.

Why do we needfloatformat?

Imagine you are running an e-commerce website, the prices of the products may range from9.90000to100.5The decimal points are uneven when the original data is displayed directly, 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.floatformatThe filter is designed to solve these problems, it can make your digital information:

  1. More organized and uniform:Ensure that all numbers of the same type are displayed with the same decimal places.
  2. More professional and credible:Standardized number formats enhance the professionalism and trustworthiness of the website.
  3. Easier to read:Avoid long decimal places so that users can catch the key information at a glance.
  4. 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 you want to format (can also be a numeric string).
  • [位数]Is an optional parameter used to specify the number of decimal places you want to keep.

Next, we will understand its different usages through specific scenarios.

1. No parameter usage (default behavior)

When you do notfloatformatThe filter takes the default smart display strategy when any parameter is provided:

  • Round the floating point number to one decimal place.
  • If the decimal place of the result is0It 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.00000After processing, it will be displayed directly.34while34.26000It will be rounded to34.3.

2. Specify a positive number parameter (fixed decimal places)

When you providefloatformata positive integer parameter for the filter (for example2or3It will round off the floating point number to the specified number of decimal places and ensure that it always displays this many digits, padding with zeros if necessary.

{{ 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.00000All digits after the decimal point are zero, in the specified3After the specified decimal places, they will also be filled in as34.000.

3. Specify the parameters0(Rounded to the nearest integer)

When you explicitly set the parameters to0then,floatformatThe filter rounds 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 in scenarios where it is necessary to display whole numbers, such as product quantities, tickets, etc.

4. Specify negative parameters (intelligent retention of decimal places)

Although the document mentions that negative number parameters are 'calculated from the last digit', the behavior seems more like rounding the floating-point number to the specified positive number of decimal places and then intelligently removing the trailing zeros.

`twig {{ 34.23234|floatformat:"-3" }} {{ 34.00000|floatformat:"-3" }} {{ 34.26000