In website content management, we often need to display various numeric information, such as product prices, discount percentages, and statistical data, etc.These numbers may sometimes have complex decimal places, and if displayed directly, they may not be very beautiful or easy to read.AnQiCMS (AnQiCMS) provides a powerful and flexible template engine, which draws on the syntax characteristics of Django templates, and can conveniently format numbers through built-in filter (filter) functions, especially for the precision control of floating-point numbers.
Easily achieve floating-point precision control:floatformatFilter
The control of display precision for floating-point numbers,floatformatThe filter is a very practical tool. It helps us retain the specified number of decimal places according to our needs and provides some default behaviors to make the display of numbers more standardized.
When you use it in the templatefloatformatthe filter, if no parameters are provided, it will default to retaining one decimal place and will automatically remove the trailing zeros. This means that, like34.23234Such numbers will be displayed as34.2while like34.00000will be displayed in a concise manner34.
{{ 34.23234|floatformat }} {# 显示结果:34.2 #}
{{ 34.00000|floatformat }} {# 显示结果:34 #}
If you need to control the number of decimal places more accurately, you can setfloatformatThe filter provides a positive integer parameter. This parameter determines the number of decimal places to be retained.
{{ 34.23234|floatformat:3 }} {# 显示结果:34.232 #}
{{ 34.00000|floatformat:3 }} {# 显示结果:34.000 #}
{{ 39.56000|floatformat:0 }} {# 显示结果:40 (会进行四舍五入)#}
It is worth noting that,floatformatThe filter can handle floating-point data types, if your template variable is a string that looks like a number (for example"34.23234"), it can also intelligently convert and format it.
More flexible control:stringformatFilter
exceptfloatformatIn addition, Anqi CMS also provides more powerful features:stringformatFilter. This filter is based on the Go language.fmt.Sprintf()A function, which means it can achieve very fine and diverse formatting requirements, far beyond floating-point precision control.
For floating-point precision,stringformatIt can handle the same, and provide more detailed control, such as whether to fill with zeros, whether to display the plus or minus sign, etc. The most commonly used floating-point formatting method is to use%.Nfof whichNRepresents the number of decimal places you want to retain.
{{ 3.141592653|stringformat:"%.2f" }} {# 显示结果:3.14 #}
{{ 12.0|stringformat:"%.3f" }} {# 显示结果:12.000 #}
stringformatThe strength lies in its versatility. You can also use it to format integers, percentages, even output complex structures as readable strings, or perform text padding alignment.
{{ 888|stringformat:"Test: %d" }} {# 显示结果:Test: 888 #}
{{ "你好"|stringformat:"Chinese: %s" }} {# 显示结果:Chinese: 你好 #}
{{ 0.25|stringformat:"%.0f%%" }} {# 如果变量是小数,想显示百分比,可以先乘以100再格式化,或者直接格式化显示: 25% #}
When you need to add prefixes, suffixes, or require strict alignment and padding characters for advanced formatting of numbers,stringformatIt is undoubtedly the better choice.
How to choose the appropriate filter?
floatformat:It is suitable for most simple floating-point precision display needs. If you just want to quickly control the number of decimal places and accept its default zero-trimming behavior, thenfloatformatis the simplest and most user-friendly option.stringformat:When you need to control numbers more precisely and customizably, for example, to fix decimal places (even if the trailing zeros are zero), add currency symbols, percentage symbols, align text, or handle other types of formatting requirements,stringformatProvided unparalleled flexibility.
In actual projects, you can choose according to the specific situation and team habits. Usually, iffloatformatUse it first if it meets your needs, as it is more concise; consider it if you need more complex formattingstringformat.
Frequently Asked Questions (FAQ)
1.floatformatandstringformatWhat are the core differences in floating-point formatting?
floatformatPrimarily focuses on controlling the number of decimal places of floating-point numbers and defaults to omitting trailing zeros (unless a specific number of decimal places is specified). Andstringformatthen provides something closer to the decimal places in programming languages.printfThe powerful capabilities of functions, you can use%f/%getc