How can percentage data be displayed on a webpage and ensure the correct formatting of decimal places?

Calendar 👁️ 95

In website operation, we often need to display various data, among which percentage data is particularly common.Whether it is the degree of completion, growth rate, discount information, or other statistics, it is crucial to present percentages clearly and accurately, and ensure that the format of decimal places is unified, which is essential for improving user experience and website professionalism.AnQiCMS (AnQiCMS) provides a flexible template system, which can easily meet this requirement with its powerful filter function.

Data Storage: The Foundation is Key

In AnQi CMS, percentage data is usually not stored directly in the database in the form of a string like "75%", but in numerical form.This is mainly for the convenience of subsequent calculations and statistics. There are usually two storage methods:

  1. Decimal Format:For example, 75% is stored as 0.75. This is the recommended way because it is closer to the mathematical definition and is more intuitive during multiplication, division, and other operations.
  2. Integer form:For example, 75% is stored as 75. This method is also applicable in certain specific scenarios, but it is necessary to pay attention to whether it needs to be divided by 100 to restore the original proportion in the template.

In order to facilitate management and expansion, we usually willContent modelAdd custom fields to store this percentage data. For example, you can create a field named "Completion rate" (completion_rate)的Number typeField, and set it as a floating-point number, so you can store precise values like 0.75.

Percentage presentation in the template: The core is the filter.

The AnqiCMS template system adopts syntax similar to Django, where Filters are powerful tools for processing data formats. When displaying percentage data, we can usefloatformatorstringformatThese two filters are used to precisely control decimal places and add percentage signs.

Suppose we obtain a named from some content modelcompletion_ratewith a value of0.75.

Method one: usefloatformatPrecisely control decimal places

floatformatThe filter is mainly used for the formatting of floating-point numbers, it can easily retain the specified decimal places of the number.

Firstly, we need to multiply the percentage data stored as a decimal (such as 0.75) by 100 to make it an integer percentage (such as 75), and then control the decimal places.

{# 假设 item.completion_rate 的值为 0.75 #}
{% set raw_percentage = item.completion_rate * 100 %}
{# 将结果保留两位小数,并添加百分号 #}
{{ raw_percentage|floatformat:2 }}%
{# 输出结果为:75.00% #}

Herefloatformat:2Represents formatting a number to two decimal places. If you want to display no decimal, you can writefloatformat:0. If you need to dynamically control the number of decimal places, you can adjust according to the actual situationfloatformatthe following numbers.

To make the code more concise, you can also place the multiplication operation directly before the filter, the template engine of Anqi CMS supports this chained operation:

{# 假设 item.completion_rate 的值为 0.75 #}
{{ (item.completion_rate * 100)|floatformat:2 }}%
{# 输出结果为:75.00% #}

Method two: usestringformatPerform more flexible formatting

stringformatthe filter provides a language similar to Gofmt.Sprintf()The function can perform more refined string formatting on various types of data. It can directly specify decimal places and insert percentage signs in the format string.

Similarly, we first need to convert decimal percentage data to integer percentages.

{# 假设 item.completion_rate 的值为 0.75 #}
{% set raw_percentage = item.completion_rate * 100 %}
{# 使用stringformat格式化为两位小数的百分比 #}
{{ raw_percentage|stringformat:"%.2f%%" }}
{# 输出结果为:75.00% #}

Here%.2fRepresents a floating-point number formatted to two decimal places, and%%This indicates outputting a literal percentage. The advantage of this method is that it is more compact and can complete number formatting and the addition of the percentage sign in a single filter.

If your data is in integer form (for exampleitem.completion_ratestored as75), then you can directly apply the filter without multiplying by 100:

{# 假设 item.completion_rate 的值为 75 #}
{{ item.completion_rate|floatformat:2 }}%   {# 输出:75.00% #}
{{ item.completion_rate|stringformat:"%.2f%%" }} {# 输出:75.00% #}

The choice of which method to use depends on personal preference and specific needs.floatformatDirectly understandable,stringformatthen provides more powerful formatting capabilities.

Consideration of practical application scenarios.

In actual use, you may obtain percentage data from different places:

  • Custom fields in the document:As mentioned above, fields defined in the content model are accessed througharchiveDetailorarchiveListtag to obtainitem.your_percentage_field.
  • System settings or contact information:If there is percentage data in the background ofGlobal feature settingsorContact information settingsYou can use the percentage data insystemorcontactLabel acquisition, then apply the filter.
  • Other label output:Any template label that outputs a numeric value can be formatted through the aforementioned filter.

It is important to maintain consistency in the percentage display format throughout the entire website.For example, if you decide to consistently retain two decimal places, then the same formatting rules should be used in all related template fragments, so that the user can experience a sense of continuity and professionalism while reading.

Summary

The AnQi CMS template system, in conjunction with built-in filters, provides a simple and efficient solution for displaying percentage data. Whether it is throughfloatformatprecise control of decimal places, or by utilizingstringformatImplement more flexible formatting that can help us present data to users in a clear and professional manner. Mastering these skills will make your website content more refined and easy to read.


Frequently Asked Questions (FAQ)

Q1: How to store percentage data in Anqi CMS so that it can be displayed correctly on the front end?

A1: It is recommended in Anqi CMS'sContent modelIn, create a new custom field for percentage data and selectNumber type. It is best to store it in decimal form for easy calculation and formattingdecimal formatstored, for example, 75% is stored as0.75. This can be directly multiplied by 100 in the template and then appliedfloatformatorstringformatfilter.

Q2: If my percentage data is already in integer form (for example, 75 represents 75%), how should I correctly format and add the percentage sign in the template?

A2: If the data is already stored in integer form (for example75), then there is no need to multiply by 100 in the template. You can apply this field directly.floatformatorstringformatThe filter to control decimal places and add a percentage sign. For example:{{ item.your_integer_field|floatformat:2 }}%Or{{ item.your_integer_field|stringformat:"%.2f%%" }}.

Q3: I need to display a progress bar on the page, with values in percentage, how should I do that?

A3: Displaying a progress bar usually involves front-end HTML and CSS (and sometimes JavaScript). Anqi CMS template is responsible for providingValueYou can format the percentage data into pure numbers (without the percentage sign) as described in the previous text, for example{{ (item.completion_rate * 100)|floatformat:0 }}Then use this number as an attribute of an HTML element, such asdata-progress="75"Or directly insert into the content of the element. Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for example Then, use CSS (for examplewidth: {{ (item.completion_rate * 100)|floatformat:0 }}%;To dynamically set the length of the progress bar. JavaScript can be used for more complex dynamic effects or animations.

Related articles

How does the Anqi CMS template handle numeric data that may be received as strings from external APIs?

In website operation, we often encounter situations where we need to integrate data from different external APIs.These APIs provide data of various types and formats, and they are quite different, especially when dealing with numerical data. We may find that fields such as "price", "stock", or "rating", which should be numeric, are returned as strings.This not only brings trouble to the template display, it may also affect data calculation and subsequent processing.AnQi CMS, with its flexible template engine and rich data processing capabilities, provides us with an elegant solution

2025-11-08

How to extract part of the numbers from a long ID string for display or logical judgment?

In website operations, we often encounter the need to handle various long string IDs, such as product SKUs, order numbers, user IDs, etc.These ID strings usually contain multiple segments of information, and we may only need part of the numbers for displaying on the page, statistics for reports, or for some business logic judgments.AnQiCMS (AnQiCMS) strong template engine provides us with flexible tools, making it easy to meet such needs.### AnQiCMS template string processing tool: Filter AnQiCMS

2025-11-08

How to perform complex mathematical expression calculations in templates, such as exponentiation or multi-step operations?

In website content management, we often need to handle various data, and this data is not always in its final form.Sometimes, we need to perform simple addition, subtraction, multiplication, and division in templates, and sometimes even more complex mathematical operations, such as calculating squares, cubes, or handling complex expressions with multiple steps.For AnQiCMS users, the good news is that its template engine provides very flexible and powerful mathematical calculation capabilities, allowing us to easily meet these needs.The template system of AnQiCMS is based on Django template engine syntax

2025-11-08

How does `floatformat` prevent the display of the decimal part when a floating-point number is exactly an integer (such as 10.00)?

In website content operation, we often encounter situations where we need to display numbers.These numbers may come from a floating-point type in a database, but in some cases, when they are exactly integers (such as `10.00`, `50.0`), we want them to be presented in a more concise integer form, such as `10` or `50`, rather than with unnecessary decimal parts.AnQiCMS (AnQiCMS) provides powerful template tags and filters, where the `floatformat` filter is a powerful tool to solve such problems

2025-11-08

I want to convert the inventory quantity (string) to an integer for comparison, which filter should I use?

In AnQi CMS template development, we sometimes encounter situations where we need to compare data such as product inventory quantity.These quantities may be stored as strings in the content model, for example, a custom field `Stock` of a product model may store such texts as "100", "50", or even "0.5".However, simple string comparison does not meet our needs when performing numerical comparisons, for example, the string "10" will be smaller than the string "2" in string comparison (because '1' is smaller than '2'), which is obviously not consistent with the actual size of the numbers

2025-11-08

How can I avoid scientific notation when displaying floating-point numbers directly in a template?

In the AnQiCMS template, when we need to display floating-point numbers, we sometimes encounter them appearing in scientific notation, such as `1.23E+07` instead of `12,300,000`.This display method is useful in some specific scenarios, but it is often difficult for ordinary users to understand intuitively, affecting the aesthetics and readability of the page, especially in situations where the accuracy is not high but the intuitiveness is required, such as displaying product prices, statistics, etc.AnQiCMS's template engine design borrowed from Django syntax

2025-11-08

Does the `integer` filter round up or down during conversion?

In AnQi CMS template development, data transformation is a common operation, especially when dealing with numbers.We often encounter situations where we need to convert a number that may have decimals into an integer, at which point the `integer` filter comes into play.However, a common question is: when using the `integer` filter for conversion, does it perform ceiling or truncation (floor)?Understanding the working principle of the `integer` filter is crucial for ensuring the accurate display of website data.in short

2025-11-08

How can I convert and validate the age input (string) in the template?

In website operation, we often need to process user input data, especially some key numerical information, such as the age of the user.Although the final processing and strict verification of data are usually performed on the backend, preliminary conversion and verification on the frontend template level can effectively improve user experience and ensure the accuracy and rationality of page display.AnQiCMS (AnQiCMS) with its flexible template engine provides powerful tools to help us achieve this goal.

2025-11-08