How to precisely control the display of floating-point numbers in the AnQiCMS template, such as retaining two decimal places?

Calendar 👁️ 74

In website content operation, the way numbers are presented often affects user experience and the accuracy of information.Especially for floating-point numbers, such as product prices, statistics, and ratings, it is a common requirement to be accurate to several decimal places or rounded off according to business needs.AnQiCMS is a powerful template system that provides us with a flexible way to handle these data.Today, we will delve deeply into how to efficiently and accurately control the display of decimal places in AnQiCMS templates.


A brief review of AnQiCMS template syntax

AnQiCMS's template engine is designed to be very intuitive, borrowing the syntax style of similar Django templates. When we want to display data on the page, we usually use double curly braces.{{ 变量名 }}When performing logical judgments, loops, and other operations, tags wrapped in percentage signs will be used.{% 标签名 参数 %}. Besides these basic grammars, AnQiCMS also provides a series of practical "filters" that can process variables and present data in the desired format.These filters are separated by the pipe symbol|Linking to the variable to form.{{ 变量名 | 过滤器名称:参数 }}structure.


Unveiling the control tool of floating-point numbers:floatformatFilter

When we need to control the display precision of floating-point numbers,floatformatThe filter is our **choice. This filter can help format numbers to a specified decimal place and round them off.

Its basic usage is very simple, just use the pipe symbol after the variable|linksfloatformatFilter, and you can follow a parameter to specify the number of decimal places:{{ 你的浮点数变量 | floatformat:位数 }}

Let's see its specific effect through several examples:

  • Default behavior (without parameters):IffloatformatThe decimal places are not specified, it will keep one decimal place by default. But there is a feature: if the last digit of the decimal part is0, the decimal place will not be displayed. For example,{{ 34.23234 | floatformat }}It will display34.2while{{ 34.00000 | floatformat }}it will display:34. When encountering34.26000It will round to34.3.

  • Specify the number of positive digits:This is the most common scenario. When we want to accurately retain N decimal places, we pass N directly as a parameter tofloatformatIt will be filled in automatically. Even if the actual number of decimal places is insufficient,0For example, to retain two decimal places:{{ 34.23234 | floatformat:2 }}Will be displayed34.23.{{ 34.00000 | floatformat:2 }}It will display34.00.{{ 34.26500 | floatformat:2 }}It will be rounded to34.27.

  • Specify the number of negative digits: floatformatEven negative numbers can be used as parameters. When a negative number is passed, it rounds from the decimal point. For example,floatformat:-2This indicates rounding a number to the nearest hundred. For example,{{ 1234.56 | floatformat:-2 }}It will display1200.{{ 1267.89 | floatformat:-2 }}It will display1300.


The practical application scenario: taking product prices as an example

Assuming we are running an e-commerce website, in the AnQiCMS product model, we have defined a product namedPriceThe floating-point number field is used to store product prices. On the product detail page or other pages where prices need to be displayed, we hope that all prices are displayed with two decimal places.

We can call it like this in the template:<div>商品价格:¥{{ archive.Price | floatformat:2 }}</div>

HerearchiveRepresents the detail data of the current product (for example, on the product detail page,archiveIt will automatically include all fields of the current product),PriceThe value of the field is a floating-point number. It is formatted through| floatformat:2no matterarchive.Pricethe original value is199.998Or200, it will be formatted as199.99or200.00. This is particularly important when displaying precise amount information.


Some usage tips and注意事项

  • Input type compatibility: floatformatThe filter can handle standard floating-point number types in Go language (such asfloat64), and it is also very smart, even if the input is a string representation of a number (such as"123.456"),can also be parsed and formatted correctly. This makes it very flexible when processing data from databases or other sources, reducing the need for manual type conversions.
  • Front-end display and back-end data:Must remember,floatformatThe filter only affects data in:Display format in the front-end template:It will not change the original data accuracy stored in the backend database.If the business logic requires precise rounding or other numerical processing on the backend, then it should be done in the business logic layer of AnQiCMS backend (for example, through custom field processing functions), rather than relying solely on frontend template filters.
  • Chaining filters:AnQiCMS supports chained use of multiple filters in templates.This means you can apply multiple filters to a variable in succession, with each filter operating on the result of the previous filter.For example, you may want to set a default value for a possibly empty variable before formatting a floating-point number:{{ archive.Rating | default:"0.0" | floatformat:1 }}This code will first check:archive.RatingIf empty, use the following `

Related articles

How to split a line of text content (such as a tag string) into an array of individual words for processing in AnQiCMS?

In the practice of AnQiCMS content management, we often encounter scenarios where it is necessary to split a seemingly simple line of text into smaller, more independent 'words' for fine-grained processing.For example, the document tag (Tag), keyword list, or multiple values separated by a specific symbol in custom fields.The core of this requirement lies in converting a string into an array that can be traversed and manipulated individually.

2025-11-08

The `escape` and `escapejs` filters in AnQiCMS are applicable to which HTML/JS escaping scenarios?

In AnQiCMS template development, it is very important to understand and properly use escape filters to ensure website security and correct content display.The system uses a template engine syntax similar to Django, which means it takes some security measures by default when handling variable output.Today, let's talk about the `escape` and `escapejs` filters to see in which scenarios they can be used.

2025-11-08

How to quickly view the detailed structure and value of complex variables during debugging in AnQiCMS template?

During AnQiCMS template development, we often need to understand what data and structure a variable contains internally, especially when dealing with complex data objects or debugging template issues.Sometimes, direct output of a variable can only yield a simple value or error message, and cannot delve into its detailed composition.At this point, it is particularly important to master some effective methods for viewing the complete structure and value of variables.### The Challenge of AnQiCMS Template Debugging The template syntax of AnQiCMS is versatile, whether it is the system built-in `archive`

2025-11-08

In AnQiCMS template, how to determine if one number can be evenly divided by another to achieve conditional display?

In Anqi CMS template development, we often need to display content based on specific conditions, such as adding a special style to every few elements in a list or inserting separators at specific positions.When this condition is to determine whether a number can be divided by another number, Anqi CMS provides a concise and efficient solution with its powerful template engine.The Anqi CMS template system uses a syntax similar to the Django template engine, which makes it very intuitive in handling such logical judgments.To determine if a number can be evenly divided by another number

2025-11-08

How to extract specific number information from a long numeric string in AnQiCMS template?

In website operations, we often encounter scenarios where we need to handle some structured long numeric string.For example, a product code may contain the production date and batch information; an order number may imply regional codes and serial numbers; or may be a unique identifier generated by specific business logic.These long digital strings often carry rich metadata, and we may only need the numerical information at specific positions for display, filtering, or further processing.

2025-11-08

How to find the first occurrence index of a character or substring in the AnQiCMS template?

During the process of displaying website content or template development, we often encounter situations where we need to process specific text, such as checking if a keyword exists or locating the first occurrence of a character or substring.The template engine of AnQiCMS (AnQiCMS) provides a series of powerful filters (Filters) to help us complete these tasks efficiently.Today, let's discuss how to use the `index` filter to accurately find the position index of the first occurrence of a character or substring in the AnQiCMS template.Understand

2025-11-08

In AnQiCMS template, how to convert a string number into an actual `integer` or `float` type?

When working with the Anqi CMS template to display content and make logical judgments, we often encounter situations where we need to perform operations on numbers.However, data obtained from a database or content model, even if it looks like a number in the background, is sometimes passed as a string (``string``) at the template level.If this is directly performed arithmetic operations or numerical comparisons, unexpected results may occur.

2025-11-08

How to concatenate the tag array in AnQiCMS template into a string with a custom delimiter?

In AnQiCMS template development, we often need to display a set of data obtained from the database, such as the multiple tags (Tags) of articles, in a user-friendly manner rather than simply listing them.One of the most common needs is to concatenate these tags into a string with a custom separator, such as AnQiCMS's powerful template engine provides a flexible way to achieve this goal.

2025-11-08