The `floatformat` filter: how to precisely control the decimal places of floating-point number display?

Calendar 👁️ 64

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

Related articles

The `stampToDate` filter: How to format Unix timestamp in AnQiCMS template?

In AnQiCMS template design, the way data is displayed is crucial to user experience.Especially information such as dates and times, if presented in raw Unix timestamp format, is difficult for ordinary visitors to understand.Fortunately, AnQiCMS provides a very practical filter——`stampToDate`, which can help us easily convert these machine-readable number sequences into clear and friendly date and time formats.### Understand Unix Timestamp and `stampToDate` Filter First

2025-11-08

`date` filter: How to format a GoLang `time.Time` type into a specific date string?

In AnQi CMS template development, flexibly displaying dates and times is an indispensable part of content presentation.You may often need to present date information in the system or content in a specific format, such as "YYYY-MM-DD" or "MM/DD HH:MM" and the like.At this point, the `date` filter has become your powerful assistant.

2025-11-08

How to convert a numeric string to an integer or floating-point number type in AnQiCMS template?

In AnQiCMS template development, we often encounter situations where we need to handle various types of data.Sometimes, data obtained from a database or through user input, even if they represent numbers, may be treated as strings at the template layer.In this case, if direct mathematical operations or numerical comparisons are performed on these 'number strings', unexpected results or even errors may occur.Therefore, understanding how to convert a numeric string to an integer or floating-point number type in the AnQiCMS template is crucial for ensuring the accuracy of data processing and the correctness of logic

2025-11-08

The role of `escape` and `escapejs` filters in preventing XSS attacks or handling special characters?

In website content operation, the display method and security of the content are equally important.AnQi CMS is an efficient content management system that provides comprehensive considerations for website security, among which the `escape` and `escapejs` filters are important tools for us to combat network attacks and ensure the correct display of content.Understanding their role can help us better manage and publish content.

2025-11-08

How `forloop.Counter` and `forloop.Revcounter` assist in indexing operations during template loops?

In AnQiCMS template development, we often need to handle various data lists, such as article lists, product displays, or navigation menus.The `for` loop is the core tool for traversing these lists.However, simply listing data items often does not meet all needs, we may also need to know the current item being traversed in the list is which item, or how many items are left until the end of the list.

2025-11-08

`capfirst`, `lower`, `upper`, `title` filters: how to handle the need for English text case conversion?

In website content operation, the uniformity and beauty of text format are crucial for improving user experience.Especially when dealing with English text, flexibly controlling the case can help us better present information, whether it is used for headings, keywords, or body content.AnQiCMS provides a series of practical template filters to help you easily deal with various English case conversion needs.

2025-11-08

How to control the alignment of a string within a specified width using `center`, `ljust`, and `rjust` filters?

In website content operation, the way content is presented often determines the first impression and reading experience of users.How to ensure that the text information displayed in the website template is uniform, beautiful, and professional, which is a concern for many operators.AnQiCMS as a content management system that focuses on user experience and highly customizable, deeply understands this, and its powerful template engine provides a variety of practical filters to help us easily align strings.

2025-11-08

`removetags` and `striptags` filters: What is the difference and **choice** when clearing HTML tags?

When managing and presenting content in Anqi CMS, we often encounter a common need: how to elegantly handle HTML tags contained in the content.In order to display a concise summary on the article list page, provide clean meta descriptions for search engine optimization (SEO), or simply to avoid unnecessary style conflicts, removing or filtering HTML tags is a basic and important skill.Aqie CMS provides two powerful filters: `removetags` and `striptags`.

2025-11-08