In website content operation, we often encounter situations where we need to display numbers. These numbers may come from floating-point types in databases, but in some cases, when they are exactly integers (such as10.00/50.0),we hope they can be presented in a more concise integer form, such as10or50It is not necessary to carry unnecessary decimal parts. AnQiCMS (AnQiCMS) provides powerful template tags and filters, wherefloatformatThe filter is the tool to solve such problems.

Let's delve deeper into it.floatformatHow can we elegantly handle these floating-point display issues.

UnderstandingfloatformatThe core role of the filter

In the template design of AnQi CMS,floatformatIt is a very useful filter, mainly used for formatting floating-point numbers, controlling the number of decimal places displayed.It is not just a simple truncation or rounding off, its design contains intelligent processing logic for 'integer floating point numbers'.

When we use a floating-point numberfloatformatThe filter's default behavior is:

  1. When the floating-point number itself contains a non-zero decimal: It will default to retaining one decimal place. For example,{{ 34.23234|floatformat }}It will be displayed as34.2.
  2. When the floating-point number is exactly an integer: This is the key point! If the original value of the floating-point number is an integer in mathematics (such as10.000/50.00), and all the digits after the decimal point are zero, thenfloatformatIn the absence of specifying the number of decimal places, the decimal part will be automatically removed and displayed as an integer. For example,{{ 34.00000|floatformat }}will be displayed very intelligently as34.

This default intelligent processing is exactly the effect we pursue, 'avoiding the display of decimal parts'.

How to avoid the display of excessive decimal parts in floating-point numbers?

Based onfloatformatThe default behavior, if you want to display no decimal part when a floating-point number is exactly an integer, and display one decimal place when it has an actual decimal, then the simplest and most effective method is tonotfloatformatThe filter specifies any decimal place parameter.

For example, there is a variable in your templateitem.PriceIt can have the following values10.00/15.50/20.333. If you use it directly{{ item.Price|floatformat }}:

  • Whenitem.PriceIs10.00The page will display10.
  • Whenitem.PriceIs15.50The page will display15.5.
  • Whenitem.PriceIs20.333The page will display20.3.

This perfectly meets our requirements: integers are displayed simply, and non-integers retain one decimal place.

However, it should be noted that if youspecifically specify the number of decimal placesFor example{{ item.Price|floatformat:2 }}thenfloatformatThe display will strictly follow the number of digits you specify:

  • Whenitem.PriceIs10.00at that time, the page will display10.00.
  • Whenitem.PriceIs15.50at that time, the page will display15.50.
  • Whenitem.PriceIs20.333at that time, the page will display20.33(will be rounded off).

Therefore, the core is to achieve the purpose of avoiding the decimal part when a floating-point number is exactly an integerUsing without parametersfloatformatFilter.

Application scenarios and **practice

It is particularly important to optimize the display when building dynamic content websites. For example, on an e-commerce website, if the product price is an integer, it is displayed as99Than99.0More aesthetic; the integer value is displayed on a data report page.100Instead100.0It can enhance the user's reading experience.

Sometimes, you may need more fine-grained control, such as, for all non-integer values, you want to force display two decimal places, and for integer values, do not display decimals at all. In this case, you can combinefloatformatThe default behavior is implemented by other logical judgments. But for most common needs, simply using one without parametersfloatformatcan meet the needs.

For example, if we always want to keep two decimal places when there are decimals, but only show integers when there are no decimals:

{% set num = 10.00 %}
{% set num2 = 10.50 %}
{% set num3 = 10.555 %}

{# 简单地使用不带参数的 floatformat #}
<p>价格1: {{ num|floatformat }}</p> {# 输出: 10 #}
<p>价格2: {{ num2|floatformat }}</p> {# 输出: 10.5 #}
<p>价格3: {{ num3|floatformat }}</p> {# 输出: 10.6 #}

It can be seen that innum3In the example, the default one decimal place will lead to:10.555being rounded to:10.6. If you need to ensure that decimals are always displayed with two decimal places, and only remove them when they are integers, a more complex judgment logic may be required, but this goes beyondfloatformatThe ability of a single filter may need to be used firststringformatorintegerFilter combinationifto make conditional judgments with statements.

In summary, AnQi CMS'sfloatformatThe filter's intelligent handling of zero decimals when not specified provides us with a concise and efficient solution, making the digital display on the website more in line with intuitive reading habits.


Frequently Asked Questions (FAQ)

Q1: Why do I usefloatformat:2after,10.00still displayed as10.00instead of10? A1: floatformat:2Explicitly tell the system to keep two decimal places. When you specify a specific number of digits,floatformatStrictly follow this requirement for formatting, including padding zeros after integers. If you want to display an integer without a decimal point when the value is exactly an integer, and show a decimal point as needed when there is a decimal, you should use the one without parameters.{{ value|floatformat }}A filter that intelligently removes the trailing.0.

Q2: If I need to convert10.5displayed as11Or10.3displayed as10(i.e., round to the nearest integer), what should I do? A2: floatformatThe filter itself will round off decimal places. If you want to round off explicitly to the nearestIntegerIt can directly convert the value to an integer type. Anqi CMS providesintegerFilter, for example{{ 10.5|integer }}Will be displayed11,{{ 10.3|integer }}Will be displayed10. This will truncate the decimal part and round it to the nearest integer.

Q3: My numbers come from floating-point numbers, but I am sure they are always logical integers. How can I ensure that they are always displayed as integers in the template and never with decimals? A3:If you are one hundred percent sure that the number is always a logical integer (such as the quantity of goods, ID, etc.), and you do not want to display decimals in any case, the most direct and safe method is to useintegerThe filter converts it to an integer. For example{{ item.Quantity|integer }}. Even if the data source is5.00, it will be displayed directly as5.floatformat(Without parameters) Although it can achieve the purpose in most cases, if the original floating-point value is5.1It will display5.1whileintegerthe filter will display directly5Rounded. Based on your specific needs (whether to round or truncate strictly, and whether to display non-zero decimals), choose the most suitable filter.