How to format a floating-point number to a specified number of decimal places?

Calendar 👁️ 68

How to accurately control the decimal places of floating-point numbers in AnQiCMS?

In website content operation, we often encounter situations where it is necessary to display prices, statistical data, measurement results, and other floating-point numbers.However, the original floating-point number often has unnecessary decimal places, which not only affects appearance but may also reduce the readability of the data.AnQiCMS fully considers this user's demand, providing flexible and powerful template filters to help us easily implement the formatting of floating-point numbers, making data display more professional and accurate.

applyfloatformatThe filter is used for decimal place control

AnQiCMS template language comes with a very useful tool, that isfloatformatA filter. Its main function is to round floating-point numbers to the specified decimal places according to our needs.

When we only need to perform routine processing on floating-point numbers, notfloatformatThe filter retains one decimal place by default when any parameter is specified. But there is a small detail to note: if the last digit of the decimal part is 0,floatformatThe decimal part will be automatically omitted to make the number look cleaner. For example, when processing34.23then, afterfloatformatfilter processing will display as34.2; while if processing34.00it will display as34.

If we need to control the decimal places more accurately, just infloatformatAdd a colon and the number you want to keep after the filter. For example, to format a floating-point number to retain two decimal places, you can write it in the template like this:{{ 34.23234|floatformat:2 }}. This will be the output34.23Similarly, if we want to keep three decimal places, we just need to change the parameter to3, that is{{ 34.23234|floatformat:3 }}The output will be34.232.

It is worth mentioning,floatformatEven supports negative numbers as parameters. For example, when the parameter is negative,floatformat:-3It rounds off the decimal points of floating-point numbers by counting digits from the end.This feature may be used when processing certain specific financial or scientific data, but for daily decimal place control, we usually use positive parameters.

Let us experience through some examplesfloatformatThe effect:

Suppose we have a floating-point numberproductPrice = 34.23234and `taxRate = 34.0

Related articles

How to get the length of a string, array, or object?

When managing website content in AnQi CMS, we often need to flexibly adjust the page display according to the number of elements in a string, array, or collection.Whether it is to judge whether the length of the article title needs to be truncated, or to count how many documents are under a certain category, or to display the number of user comments, obtaining the 'length' of this content is the key to dynamic display and logical judgment.The AnQi CMS template engine provides built-in features that are simple and efficient, helping us easily deal with these scenarios.Among them, the `length` filter is a powerful assistant for obtaining the length of strings, arrays, or key-value pairs

2025-11-09

How to display a default value when a variable is empty?

In website operation, we often encounter such situations: a content field may not be filled in for various reasons, such as the author of the article, image description, or specific parameters of a product.If the template outputs a blank directly when displaying this content, it not only affects the aesthetics but may also confuse the user.At this time, how to display a friendly default value when a variable is empty has become a very practical skill.The AnQi CMS template system, with its flexible and powerful features, provides us with various methods to elegantly solve this problem

2025-11-09

How to convert a string to uppercase, lowercase, or title case?

In website content operation, the way content is presented often determines the overall impression of users on the website.Whether it is to maintain brand style consistency or to improve the readability of text, flexible case conversion of strings is a basic and important operation.AnQiCMS (AnQiCMS) understands this, and it has built-in simple and easy-to-use filters (Filters) in its powerful template engine, allowing you to easily perform string transformations such as uppercase, lowercase, and capitalization without complex programming.We will introduce in detail how to use the Anqi CMS template

2025-11-09

How to perform basic arithmetic operations in the template?

In Anqi CMS template design, it can flexibly handle data, perform simple calculations, and is an indispensable part of creating dynamic and functional websites.You may find that in addition to displaying the content itself, it is also necessary to perform some basic arithmetic operations at the template level, such as calculating the total price of goods, displaying the discounted price, or dynamically adjusting the style of elements based on numerical values.The AnQi CMS template engine (similar to Django syntax) provides us with intuitive and powerful arithmetic calculation capabilities, allowing these requirements to be easily implemented in the template.###

2025-11-09

How to center or align a string to a specified length?

When building website content, we often need to fine-tune the text layout to ensure that the information presented is both beautiful and clear.Whether it is a list, table, product parameters, or other structured display data, maintaining visual alignment and consistency is crucial for improving user experience.Aq CMS, known for its high flexibility and ease of use as a content management system, deeply understands the importance of content presentation and has built a series of powerful template filters to help us easily align and center strings

2025-11-09

How to determine if a string or array contains a specific keyword?

In AnQi CMS, efficiently identify key information in content: string and array keyword judgment techniques As a content operator, we often need to handle a large amount of text data, from article titles, content descriptions to custom fields, rich and diverse information.In order to optimize search engine (SEO), implement content intelligent recommendation, or perform simple data validation, quickly judge whether a string or array contains a specific keyword is a very practical and efficient skill.In the flexible and powerful template system of Anqi CMS, this task becomes effortless.Utilize built-in filters

2025-11-09

How to calculate the number of times a specific keyword appears in a string or array?

In AnQiCMS content management practice, we often need to carry out refined analysis and optimization of website content.In which, calculating the number of times a specific keyword appears in an article, title, or dataset is a basic and important requirement.In order to evaluate SEO keyword density, analyze content quality, or conduct data statistics, AnQiCMS provides convenient and efficient solutions.This article will deeply explore how to utilize the built-in features of AnQiCMS to easily achieve this goal.### Core Tool: `count`

2025-11-09

How to remove specific characters or HTML tags from a string?

In the daily content operation of AnQiCMS, we sometimes encounter situations where we need to finely process the content, such as removing specific characters, extra spaces, or stripping unnecessary HTML tags from the content.These operations are very critical for ensuring the neatness of content display, adapting to different front-end styles, and even for data cleaning.The AnQi CMS provides powerful template filter functions to help us efficiently complete these tasks.###

2025-11-09