In website content operation, the presentation of numbers often affects user experience and the accuracy of information.Especially for floating-point numbers, such as product prices, statistical data, ratings, etc., it is a common requirement to be precise to a few decimal places or rounded off according to business needs.AnQiCMS's powerful template system provides us with a flexible way to handle this data.Today, let's delve into how to efficiently and accurately control the number of decimal places for floating-point numbers in the AnQiCMS template.
A brief review of AnQiCMS template syntax
The template engine of AnQiCMS is designed to be very intuitive, borrowing the syntax style similar to 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, labels enclosed in percentage signs will be used{% 标签名 参数 %}.In addition to these basic syntaxes, AnQiCMS also provides a series of practical 'filters' that can process variables and present data in the format we expect.|Linked to the variable to form{{ 变量名 | 过滤器名称:参数 }}structure.
Unveiling the floating-point control tool:floatformatFilter
When we need to control the display precision of floating-point numbers,floatformatThe filter is our **choice. This filter can help us format numbers to a specified number of decimal places and round them.
Its basic usage is very simple, just through the pipe symbol after the variable|linkagefloatformatfilter, and you can follow a parameter to specify the number of decimal places:{{ 你的浮点数变量 | floatformat:位数 }}
Let's take a look at its specific effect through several examples:
Default behavior (without parameters):If
floatformatThe number of decimal places is not specified, it will default to one decimal place. However, there is a little feature: if the last digit of the decimal part is0then the decimal place will not be displayed. For example,{{ 34.23234 | floatformat }}will be displayed34.2while{{ 34.00000 | floatformat }}it will display:34. When encountering34.26000When, it will round to34.3.Specify the number of positive digits:This is the most common scenario. When we want to retain N decimal places accurately, we pass N as a parameter directly to
floatformatIt will be automatically filled. Even if the actual number of decimal places is insufficient, it will be filled in.0For example, to keep two decimal places:{{ 34.23234 | floatformat:2 }}Will be displayed34.23.{{ 34.00000 | floatformat:2 }}will be displayed34.00.{{ 34.26500 | floatformat:2 }}will be rounded to34.27.Specify the number of negative digits:
floatformatEven supports negative numbers as parameters. When a negative number is passed, it rounds from the decimal point forward. For example,floatformat:-2Represents rounding a number to the nearest hundred. For example,{{ 1234.56 | floatformat:-2 }}will be displayed1200.{{ 1267.89 | floatformat:-2 }}will be displayed1300.
Application scenario: Taking product prices as an example
Assuming we are operating an e-commerce website, in the product model of AnQiCMS, we defined a namedPriceThe floating-point field, used to store product prices. On the product detail page or any page that needs to display prices, we hope that all prices are uniformly retained to two decimal places.
We can call it like this in the template:<div>商品价格:¥{{ archive.Price | floatformat:2 }}</div>
Here are thearchiveRepresents the detailed data of the current product (for example, on the product detail page,)archiveand will automatically include all fields of the current product),PriceThe value of the field is a floating-point number. Through| floatformat:2。“Regardlessarchive.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 precautions
- Compatibility of input types:
floatformatFilters can not only handle standard floating-point number types in Go language (such asfloat64),It is also very intelligent, even if the input is a string representation of a number (such as"123.456"It can also be correctly parsed and formatted. This makes it very flexible when handling data from databases or other sources, reducing the need for manual type conversions. - Front-end display and back-end data:Remember to do so,
floatformatThe filter only affects the data inthe display format in the front-end templateThe value 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, it should be done in the business logic layer of AnQiCMS backend (such as through custom field processing functions), rather than relying solely on frontend template filters. - Chaining filter usage:AnQiCMS template supports the chained use of multiple filters.This means you can apply multiple filters sequentially to a variable, with each filter operating on the result of the previous one.
{{ archive.Rating | default:"0.0" | floatformat:1 }}This code will first checkarchive.Ratingif it is empty, if it is empty then use '`