In AnQi CMS, the details of content display often determine the quality of user experience.How to present common numbers on the website, especially floating-point numbers, in a clear, accurate, and expected format is a concern for content operators.floatformatThe filter is a powerful tool for handling the display of floating-point numbers.

Today, we will delve into it together.floatformatA special usage of the filter: does it support negative digit places, and how does it 'discard decimal places from right to left' in this case?

floatformatFilter: Overview of basic functions

First, let's reviewfloatformatThe basic function of the filter. Its main role is to format numbers into floating-point representations with specified decimal places.

  • When no parameters are specified: When you do notfloatformatThe filter will default to rounding floating-point numbers to one decimal place. It is worth noting that if rounded to one decimal place, the last digit is0If it is smart enough not to display the decimal point anymore0. For example:{{ 34.23234|floatformat }}The result is34.2 {{ 34.00000|floatformat }}The result is34 {{ 34.26000|floatformat }}The result is34.3(Here it has been rounded off)

  • When specifying the number of significant digits for a positive numberWhen you need to accurately retain a specified number of decimal places, you can directly provide a positive integer as a parameter. For example,floatformat:Nthe decimal number will be precise to the decimal point afterNDigits, and follow the same rounding rules.{{ 34.23234|floatformat:3 }}The result is34.232 {{ 34.00000|floatformat:3 }}The result is34.000

  • specified0when the number of digits is: If you want to truncate the floating-point number directly without retaining any decimal places, you can pass a string"0"as a parameter.{{ 34.23234|floatformat:"0" }}The result is34 {{ 39.56000|floatformat:"0" }}The result is40(Round off follows)

We can see from these basic usagefloatformatThe flexibility in handling decimal place accuracy and rounding off.

The mystery of negative number places: Is it really 'discard from right to left'?

Now, let's focus on this interesting question:floatformatDoes the filter support negative digit numbers and does its behavior conform to our usual understanding of 'discard decimal places from right to left'?

According to the relevant document of AnQi CMS,floatformatFilterDoes indeed support negative number of digitsThe document mentions: "If a negative number is set, it is calculated from the last digit."

However, when we look at the provided examples, we notice a significant detail:{{ 34.23234|floatformat:"-3" }}The result is34.232

This result is consistent with{{ 34.23234|floatformat:3 }}the result (which is the same as34.232) is completely consistent. This means that,floatformatIn the filter, when you enter a negative number-NThe actual effect isRetain the decimal places afterNBit, rather than like some programming languages (such as Python'sround(12345, -2)You will get12300As such, the negative bit parameter is used to discard the integer part.

Therefore, in Anqi CMS,floatformatThe way the filter handles the negative bit parameter can be understood as: whether it is specifiedN(positive) or"-N"Negative numbers, their core function is to round off floating-point numbers to the decimal pointNPositions, and appropriate rounding should be performed. The negative sign is more of a grammatical support here, but it behaves consistently with the corresponding positive number of decimal places in terms of controlling the precision of decimal places.

For example, if you want to format123.45678this number to two decimal places, whether using{{ 123.45678|floatformat:2 }}Or{{ 123.45678|floatformat:"-2" }}, the result will be123.46. It will not affect the integer part, changing it to100or similar integer discarding results.

Examples of actual application scenarios

Even though the behavior of negative number digits may differ from the intuition of some users,floatformatThe filter is still very useful in the following scenarios:

  • Financial data displayIn e-commerce websites or financial statements, the numbers such as product prices, tax rates, and profits are usually required to be accurate to two decimal places or more,floatformat:2orfloatformat:3which can well meet these needs.
  • Science and statisticsWhen displaying experimental results, statistical averages, or measured data, it may be necessary to retain different decimal places according to the accuracy requirements.
  • Unified number formatEnsure that certain numbers on the website (such as product ratings, discount percentages) are always displayed with a uniform number of decimal places to enhance the professionalism and readability of the content.
  • Simple rounding off: Use.floatformat:"0"Quickly truncate floating-point numbers, suitable for scenarios where decimals are not needed, such as user counts, inventory quantities, and so on.

Use suggestions and precautions