In website content operation, the way numbers are presented often affects user experience and the accuracy of information.Especially for floating-point numbers, such as product prices, statistics, and ratings, it is a common requirement to be accurate to several decimal places or rounded off according to business needs.AnQiCMS is a powerful template system that provides us with a flexible way to handle these data.Today, we will delve deeply into how to efficiently and accurately control the display of decimal places in AnQiCMS templates.


A brief review of AnQiCMS template syntax

AnQiCMS's template engine is designed to be very intuitive, borrowing the syntax style of similar Django templates. When we want to display data on the page, we usually use double curly braces.{{ 变量名 }}When performing logical judgments, loops, and other operations, tags wrapped in percentage signs will be used.{% 标签名 参数 %}. Besides these basic grammars, AnQiCMS also provides a series of practical "filters" that can process variables and present data in the desired format.These filters are separated by the pipe symbol|Linking to the variable to form.{{ 变量名 | 过滤器名称:参数 }}structure.


Unveiling the control tool of floating-point numbers:floatformatFilter

When we need to control the display precision of floating-point numbers,floatformatThe filter is our **choice. This filter can help format numbers to a specified decimal place and round them off.

Its basic usage is very simple, just use the pipe symbol after the variable|linksfloatformatFilter, and you can follow a parameter to specify the number of decimal places:{{ 你的浮点数变量 | floatformat:位数 }}

Let's see its specific effect through several examples:

  • Default behavior (without parameters):IffloatformatThe decimal places are not specified, it will keep one decimal place by default. But there is a feature: if the last digit of the decimal part is0, the decimal place will not be displayed. For example,{{ 34.23234 | floatformat }}It will display34.2while{{ 34.00000 | floatformat }}it will display:34. When encountering34.26000It will round to34.3.

  • Specify the number of positive digits:This is the most common scenario. When we want to accurately retain N decimal places, we pass N directly as a parameter tofloatformatIt will be filled in automatically. Even if the actual number of decimal places is insufficient,0For example, to retain two decimal places:{{ 34.23234 | floatformat:2 }}Will be displayed34.23.{{ 34.00000 | floatformat:2 }}It will display34.00.{{ 34.26500 | floatformat:2 }}It will be rounded to34.27.

  • Specify the number of negative digits: floatformatEven negative numbers can be used as parameters. When a negative number is passed, it rounds from the decimal point. For example,floatformat:-2This indicates rounding a number to the nearest hundred. For example,{{ 1234.56 | floatformat:-2 }}It will display1200.{{ 1267.89 | floatformat:-2 }}It will display1300.


The practical application scenario: taking product prices as an example

Assuming we are running an e-commerce website, in the AnQiCMS product model, we have defined a product namedPriceThe floating-point number field is used to store product prices. On the product detail page or other pages where prices need to be displayed, we hope that all prices are displayed with two decimal places.

We can call it like this in the template:<div>商品价格:¥{{ archive.Price | floatformat:2 }}</div>

HerearchiveRepresents the detail data of the current product (for example, on the product detail page,archiveIt will automatically include all fields of the current product),PriceThe value of the field is a floating-point number. It is formatted through| floatformat:2no matterarchive.Pricethe original value is199.998Or200, it will be formatted as199.99or200.00. This is particularly important when displaying precise amount information.


Some usage tips and注意事项

  • Input type compatibility: floatformatThe filter can handle standard floating-point number types in Go language (such asfloat64), and it is also very smart, even if the input is a string representation of a number (such as"123.456"),can also be parsed and formatted correctly. This makes it very flexible when processing data from databases or other sources, reducing the need for manual type conversions.
  • Front-end display and back-end data:Must remember,floatformatThe filter only affects data in:Display format in the front-end template:It will not change the original data accuracy stored in the backend database.If the business logic requires precise rounding or other numerical processing on the backend, then it should be done in the business logic layer of AnQiCMS backend (for example, through custom field processing functions), rather than relying solely on frontend template filters.
  • Chaining filters:AnQiCMS supports chained use of multiple filters in templates.This means you can apply multiple filters to a variable in succession, with each filter operating on the result of the previous filter.For example, you may want to set a default value for a possibly empty variable before formatting a floating-point number:{{ archive.Rating | default:"0.0" | floatformat:1 }}This code will first check:archive.RatingIf empty, use the following `