How to round numbers or retain a specified number of decimal places in AnQiCMS templates?

Calendar 👁️ 68

In the presentation of website content, the accuracy and readability of numbers are crucial for enhancing user experience and the effectiveness of information communication.Whether it is to display product prices, financial data, or various statistical indicators, we often need to round numbers or retain specific decimal places.As a template developer or content operator for AnQiCMS, understanding how to flexibly control the display format of numbers is a crucial step in achieving professional content presentation.

AnQiCMS's template system adopts the syntax of Django template engine, which means it provides a powerful filter (Filters) mechanism.We can apply various processing and formatting to the variable output through these filters.For rounding and decimal place control of numbers, AnQiCMS provides a very practical and powerful built-in filter:floatformat.

Core Tool:floatformatFilter

floatformatThe filter is designed specifically for floating-point numbers (i.e., numbers with decimals), it can flexibly control the number of decimal places displayed in the number, and automatically performs rounding operations.

  1. Basic usage: Intelligent rounding and decimal truncationWhen you do notfloatformatThe filter shows intelligent behavior when any parameters are specified.If the decimal part of a number is all zeros, it will only display the integer part without a decimal point;If the decimal part is not zero, it will round to the nearest one decimal place.

    For example, assume you have a variablevalue:

    {{ value|floatformat }}
    
    • WhenvalueWith34.23234The output will be34.2.
    • WhenvalueWith34.00000The output will be34.
    • WhenvalueWith34.26000The output will be34.3.

    This default behavior is very convenient in many scenarios, allowing numbers to be displayed both accurately and concisely.

  2. Specify the number of decimal places: precise control of display formatIf you need to control the number of decimal places more accurately, for example, always display two decimal places even if the decimal part of the number is zero, you can do so byfloatformatThe filter passes an integer parameter to be implemented. This parameter represents the number of decimal places you want to retain.

    For example, to format a number to retain three decimal places:

    {{ value|floatformat:3 }}
    
    • WhenvalueWith34.23234The output will be34.232.
    • WhenvalueWith34.00000The output will be34.000.
    • WhenvalueWith34.26000The output will be34.260.

    in this mode,floatformatEnsure that the output number always has the specified number of decimal places, and zeros will be automatically added if it is insufficient.

  3. Use negative parameters: advanced rounding requirements floatformatThe filter also supports using negative numbers as parameters, which is very useful in certain financial or statistical scenarios. The meaning of negative parameters is to round from the end of the number. For example

Related articles

Does AnQiCMS support displaying collected articles directly on the website?

## AnQiCMS content collection feature: let the article go online directly, or go further?In the era of information explosion, continuously delivering high-quality content to websites is a major challenge faced by many operators.The efficiency of content production is directly related to the activity of the website, user stickiness, and even search engine rankings.

2025-11-09

How to convert the case of a string and display it in AnQiCMS template?

In website content operation, we often encounter the need to format text.In order to unify the brand image, improve the readability of the page, or optimize the search engine friendliness, converting strings to uppercase and lowercase is a fundamental and practical skill.AnQiCMS as an enterprise-level content management system developed based on the Go language, with its flexible template engine syntax (similar to Django templates), provides us with a rich set of tools to accurately control the display of content.Today, let's delve deeply into how to use AnQiCMS

2025-11-09

How does the AnQiCMS website achieve fast content display under high concurrency through static caching and Go language features?

In today's rapidly changing digital world, the speed of website access and stability under high concurrency have become key factors in user experience and search engine rankings.For a content-rich corporate website or self-media platform, ensuring that content is presented quickly while efficiently utilizing server resources is a major challenge for operators.AnQiCMS (AnQiCMS) with its powerful core based on the Go language and the exquisite static caching mechanism, provides an excellent solution to this problem.### Go Language Empowerment

2025-11-09

How to set up an independent template for a single page in AnQiCMS and display its content correctly?

In AnQiCMS, single pages (such as "About Us", "Contact Information", or specific product landing pages) are an important part of our website content.They usually carry unique information and need to have a different layout and visual style from other content on the website (such as article lists or product details).Fortunately, AnQiCMS provides极高的flexibility, allowing you to easily set up a dedicated independent template for each single page, thereby creating a unique user experience.### AnQiCMS template base

2025-11-09

How to display the related article list of the `tagDataList` tag in AnQiCMS according to the specified Tag ID?

Manage and display content in AnQi CMS, especially when it is necessary to aggregate articles based on specific themes or keywords, the Tag feature is particularly important.It can not only help visitors quickly find the content they are interested in, but also effectively improve the internal link structure and SEO performance of the website.Today, let's delve into a very practical template tag in AnQiCMS: `tagDataList`, and see how it accurately displays a list of related articles based on the specified Tag ID.

2025-11-09

How to add captcha functionality to the AnQiCMS comment form to reduce the display of spam comments?

In website operation, spam comments are undoubtedly a headache.They not only affect the user experience of the website, dilute high-quality discussion content, but may also harm the website's SEO performance in an invisible way, even occupying valuable server resources.Faced with increasingly intelligent spam comment robots, we need more effective defense measures.Fortunately, AnQiCMS provides a simple and powerful solution - adding a captcha feature to the comment form, which can significantly reduce the intrusion of spam comments.

2025-11-09

How to automatically replace and display anchor text in article content in AnQiCMS?

In website operation, anchor text is an important element that connects different pages, enhances content relevance, and optimizes search engine rankings.A well-crafted anchor text can not only guide users to discover more relevant information but also help search engines better understand the content and structure of the page and website.For those of you who use AnQiCMS (AnQiCMS) to manage content, the system provides an efficient and intelligent mechanism to automatically convert keywords in the article content into linked anchor text, thereby greatly improving the efficiency and effectiveness of content operation.

2025-11-09

How to optimize the page layout display using AnQiCMS template inheritance (`extends`) and inclusion (`include`) tags?

In website construction, how to organize the page structure efficiently to make the code reusable and easy to maintain is the key to improving development efficiency and project quality.AnQiCMS has fully considered this point, providing powerful template inheritance (`extends`) and template inclusion (`include`) tags, allowing us to build websites like building blocks, easily realizing complex page layout and display optimization.

2025-11-09