Easily achieve floating-point precision control:floatformatFilter
For floating-point number display precision control,floatformatThe filter is a very practical tool. It can help 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 usefloatformatfilters in templates, if no parameters are provided, it will default to retaining one decimal place and automatically removing trailing zeros. This means that, like34.23234will be displayed as34.2, while like34.00000Then it will 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 canfloatformatFilter 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 number types, and 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
Exceptfloatformat之外,English CMS还提供了功能更强大的stringformat过滤器。这个过滤器基于English语言的fmt.Sprintf()The function, which means it can achieve very fine and diverse formatting requirements, far beyond floating-point number precision control.
Regarding floating-point number precision,stringformatSimilarly capable, and provides more detailed control, such as whether to pad with zeros, whether to display the sign, etc. The most commonly used floating-point number formatting method is to use%.Nfwhere,NRepresents the number of decimal places you want to keep.
{{ 3.141592653|stringformat:"%.2f" }} {# 显示结果:3.14 #}
{{ 12.0|stringformat:"%.3f" }} {# 显示结果:12.000 #}
stringformatThe strength lies in its versatility.In addition to floating-point numbers, you can also use it to format integers, percentages, even to output complex structures as readable strings, or to perform text filling and alignment.
{{ 888|stringformat:"Test: %d" }} {# 显示结果:Test: 888 #}
{{ "你好"|stringformat:"Chinese: %s" }} {# 显示结果:Chinese: 你好 #}
{{ 0.25|stringformat:"%.0f%%" }} {# 如果变量是小数,想显示百分比,可以先乘以100再格式化,或者直接格式化显示: 25% #}
When you need to perform advanced formatting such as adding prefixes, suffixes, or requiring strict alignment and padding characters for numbers.stringformatIt is undoubtedly the better choice.
How to choose an appropriate filter?
floatformat:It is suitable for most simple floating-point precision display requirements. If you just want to quickly control the number of decimal places and accept its default zero removal behavior, thenfloatformatis the simplest and most user-friendly choice.stringformat:When you need to control numbers more precisely and customizably, such as fixing decimal places (even if the trailing zeros are there), adding currency symbols, percentage symbols, aligning text, or handling other types of formatting requirements,stringformatProvided unparalleled flexibility.
In actual projects, you can choose according to specific situations and team habits. Usually, iffloatformatIt can meet your needs, so use it first as it is more concise; consider it if you need more complex formattingstringformat.
Common Questions (FAQ)
1.floatformatandstringformatWhat are the core differences in floating-point formatting?
floatformatEnglish translation: The main focus is on controlling the decimal places of floating-point numbers and it will omit trailing zeros by default (unless a specific number of decimal places is specified). Andstringformatwhich provides more in line with the programming language.printfThe powerful ability of functions, you can use%f/%gWhy