How does `floatformat` prevent the display of the decimal part when a floating-point number is exactly an integer (such as 10.00)?

Calendar 👁️ 75

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.

Related articles

How to dynamically adjust the decimal precision of displayed numbers in a template?

In website content operation, the precise display of numbers is often crucial, whether it is product prices, statistical data, or technical indicators, the accuracy of decimal point display directly affects the accuracy of information and the understanding of users.AnQiCMS provides a flexible template engine that allows us to easily adjust the decimal precision of numbers in frontend templates, thus satisfying various complex display requirements.The AnQiCMS template system is developed based on Go language, with a syntax style similar to Django, it offers powerful Filter (Filter) functions

2025-11-08

How can I convert a string price from a custom field into a number that can be used in the order total in the template?

In website operation, we often encounter the need to handle numerical data such as product prices or service charges.This data is sometimes stored in custom fields, but for various reasons, they may be saved in the form of strings (text), such as "199.50 yuan" or "150".When we need to perform a total calculation on these prices in a template (such as the order total), directly performing a 'sum' operation on the string will yield unexpected results (such as '199.5015.00' instead of '214.50').At this point, it is necessary to convert these string prices into actual numbers

2025-11-08

How can I perform numerical calculations based on a user-defined parameter (string)?

During website operation, we often encounter scenarios where we need to perform calculations on custom data, such as dynamically calculating prices based on product parameters, displaying rating statistics, or performing other numerical processing based on user input or content attributes.AnQi CMS provides us with the ability to meet these requirements with its flexible content model and powerful template engine.This article will introduce in detail how to perform numerical calculations based on custom string parameters in Anqi CMS.### First Step: Preparation - Define Your Custom Parameters First

2025-11-08

How to display product prices in AnQiCMS templates and control decimal places?

In website operation, the display of product prices is a crucial link, not only to ensure the accuracy of information, but also to ensure that the presentation method conforms to the reading habits and brand image of users.AnQiCMS with its flexible template system, provides users with powerful customization capabilities, allowing you to easily control the display of product prices, including decimal places.### How to get product price: Where does it come from? In the AnQiCMS template, the product price is usually stored and called as a field of the document (Article or Product)

2025-11-08

How to perform complex mathematical expression calculations in templates, such as exponentiation or multi-step operations?

In website content management, we often need to handle various data, and this data is not always in its final form.Sometimes, we need to perform simple addition, subtraction, multiplication, and division in templates, and sometimes even more complex mathematical operations, such as calculating squares, cubes, or handling complex expressions with multiple steps.For AnQiCMS users, the good news is that its template engine provides very flexible and powerful mathematical calculation capabilities, allowing us to easily meet these needs.The template system of AnQiCMS is based on Django template engine syntax

2025-11-08

How to extract part of the numbers from a long ID string for display or logical judgment?

In website operations, we often encounter the need to handle various long string IDs, such as product SKUs, order numbers, user IDs, etc.These ID strings usually contain multiple segments of information, and we may only need part of the numbers for displaying on the page, statistics for reports, or for some business logic judgments.AnQiCMS (AnQiCMS) strong template engine provides us with flexible tools, making it easy to meet such needs.### AnQiCMS template string processing tool: Filter AnQiCMS

2025-11-08

How does the Anqi CMS template handle numeric data that may be received as strings from external APIs?

In website operation, we often encounter situations where we need to integrate data from different external APIs.These APIs provide data of various types and formats, and they are quite different, especially when dealing with numerical data. We may find that fields such as "price", "stock", or "rating", which should be numeric, are returned as strings.This not only brings trouble to the template display, it may also affect data calculation and subsequent processing.AnQi CMS, with its flexible template engine and rich data processing capabilities, provides us with an elegant solution

2025-11-08

How can percentage data be displayed on a webpage and ensure the correct formatting of decimal places?

In website operation, we often need to display various data, among which percentage data is particularly common.Whether it is the completion, growth rate, discount information or other statistics, it is crucial to present percentages clearly and accurately, and ensure uniformity in the format of decimal places, which is essential for improving user experience and website professionalism.AnQiCMS (AnQiCMS) provides a flexible template system, which can be very convenient to meet this requirement with its powerful filter function.### Data storage: the foundation is crucial In AnQi CMS

2025-11-08