How to format floating-point numbers and retain a certain number of decimal places in AnQiCMS templates?

Calendar 👁️ 76

In website content operation, we often need to display floating-point number information such as prices, percentages, and ratings.The accuracy and display method of these numbers directly affect user experience and the professionalism of information.AnQi CMS understands this need and provides a powerful and flexible floating-point number formatting function in the template engine, allowing you to easily control the display accuracy of floating-point numbers.

Next, we will explore two main methods of formatting floating-point numbers and retaining specified decimal places in AnQiCMS templates:floatformatFilters andstringformatfilter.

UsefloatformatFilter performs floating-point number formatting

floatformatThe filter is a convenient tool used specifically for floating-point number formatting in the AnQiCMS template.Its main function is to round off floating-point numbers according to the specified requirements and control the decimal places.

Default usage:

If no parameters are specified,floatformatThe filter attempts to keep floating-point numbers to one decimal place and intelligently remove trailing zeros. This means that like34.0Such numbers will be displayed as34while34.23234It will be displayed as34.2.

{% set price = 123.4567 %}
{% set quantity = 5.0 %}
{% set rate = 34.26000 %}

<p>原价:{{ price | floatformat }}</p> {# 结果:123.5 #}
<p>数量:{{ quantity | floatformat }}</p> {# 结果:5 #}
<p>转换率:{{ rate | floatformat }}</p> {# 结果:34.3 #}

Specify the number of decimal places:

When you need to control the number of decimal places accurately, you canfloatformatAdd a number to indicate the number of decimal places you want to keep. In this case, even if the decimal part is zero, it will be automatically filled out to the specified number of places.

`twig Set discount rate to 0.8 Set score to 98.7654 Set total amount to 99.9

Discount: {{ discountRate | floatformat:2 }}<p>

Related articles

How to automatically wrap long text in AnQiCMS templates for optimized reading experience?

In website content operation, optimizing the reading experience is a key factor in attracting and retaining users.Especially for long text content, if there is a lack of proper formatting and automatic line breaking, users may face issues such as horizontal scrolling and text stacking, which can severely affect reading comfort and even lead to user loss.As an AnQiCMS user, we can take advantage of its powerful template engine and built-in features to easily implement automatic line breaks in long text, thereby greatly enhancing the reading experience of the website content.## The Importance of Understanding Long Text Line Breaks Imagine browsing an article on your phone

2025-11-07

How to define an array and display its content in the template using the AnQiCMS filter?

## Advanced Template Development: Flexibly Define and Display Array Content in AnQiCMS During the template development process of AnQiCMS, we often encounter scenarios where we need to process list data, dynamic configuration options, or generate a series of contents based on specific logic.Although most of the data is obtained through tags from the backend, in some cases, defining and operating on simple data sets directly in the template, especially arrays, can greatly improve the flexibility and efficiency of front-end development and reduce dependence on backend data.

2025-11-07

How to extract a specified part of a string or array for display in the AnQiCMS template?

In AnQiCMS template development, we often encounter the need to handle string or array content, such as displaying article summaries, limiting the number of image lists, or extracting specific information from a long text.The AnQi CMS is developed based on the Go language, its template engine supports syntax similar to Django and Blade, and provides a rich set of filters (filters) to help us efficiently complete the content extraction and display requirements.Let's take a look at how to flexibly extract the specified part of a string or array for display in the AnQiCMS template.

2025-11-07

How to link array elements into a string for display in AnQiCMS template?

In AnQiCMS template development, we often encounter situations where we need to display a series of related data, such as multiple tags of an article, various characteristics of a product, or multiple options stored in a custom field.These data are often present in the template in the form of an array, and we hope to display them in a concise and beautiful string format, such as connecting them with commas, slashes, or other symbols.

2025-11-07

How to get the thumbnail of AnQiCMS image resources and display it?

AnQiCMS focuses on visual effects and page loading efficiency in content display, therefore, how to efficiently obtain and display thumbnail images of image resources is a common problem we encounter in daily operations.It is fortunate that AnQiCMS provides a very convenient and flexible way to handle image thumbnails, allowing us to easily meet various display needs. --- ### How to manage and generate image thumbnails in AnQiCMS?

2025-11-07

How to parse and output HTML code in AnQiCMS template instead of escaping it?

When using AnQi CMS for website content creation and template design, we often encounter a problem related to HTML code display: Why is the HTML code I enter in the background editor displayed as plain text on the front page instead of being parsed by the browser as actual HTML elements?This is actually the content management system that defaults to enabling HTML content escaping for website security.This article will delve into how to effectively control the output of HTML code in the AnQiCMS template, ensuring that it is parsed correctly and not displayed as escaped.

2025-11-07

How to get and display detailed information of user groups in AnQiCMS template?

In AnQiCMS, user group management is one of the core functions for building differentiated services and content monetization systems.It is crucial to display detailed information about user groups accurately in the front-end template, whether it is to provide exclusive content for VIP users or to set access restrictions for users of different permission levels.This not only improves the user experience, but also effectively guides users to understand and upgrade their own permissions.How can we flexibly obtain and display the detailed information of these user groups in the AnQiCMS template?

2025-11-07

How to display the language switch options and hreflang tags on a multilingual site in AnQiCMS?

Building multilingual websites in AnQiCMS and providing convenient language switch options as well as correct hreflang tags is a key step in expanding the international market and improving user experience.AnQiCMS with its flexible architecture provides a clear path to achieve this goal, allowing your website content to accurately reach global users while ensuring search engine friendliness. ### Overview of AnQiCMS Multilingual Implementation AnQiCMS integrates multilingual support with multi-site management functions in its design.This means, each different language version

2025-11-07