In the template of Anqi CMS, handling the display of floating-point numbers, especially hoping to remove the unnecessary zeros after the numbers, is an actual need that many website operators encounter. When our product prices are12.5000,or a certain statistical number is34.00at this time, these extra zeros are not only not beautiful, but may also confuse visitors. At this time, the built-infloatformatthe filter can come into play.
floatformatHow can the filter prevent extra zeros from displaying for floating-point numbers?
floatformatThe filter is used in the template rendering of AnQi CMS to format floating-point numbers.It stands out for its ability to intelligently handle trailing zeros in floating-point numbers by default, even removing the decimal point when the number is an integer, presenting the number in the simplest and most natural form.
When used directly like this{{ 你的浮点数 | floatformat }}When, the filter will default to retaining one decimal place.If the fractional part is all zeros, or if the number is rounded and there are no significant digits, it will automatically omit all these zeros and the decimal point.
- If your variable is
priceThe value of12.50000Use{{ price | floatformat }}it will be displayed as12.5. - If
priceThe value of34.00000Use{{ price | floatformat }}it will be displayed as34, the decimal point is also removed. - If
priceThe value of5.678and it will be displayed as rounded due to the rounding.5.7.
This default processing method is the ideal solution we hope for to prevent floating-point numbers from displaying unnecessary zeros, making the numbers on the page look cleaner and more professional.
Of course,floatformatAlso provides more fine-grained control options. If you have specific requirements, and you want to display a floating-point number with a fixed number of decimal places, even if the trailing zeros are zero, you can achieve this by passing a numeric parameter to the filter, for example{{ 你的浮点数 | floatformat:2 }}. At this point, its behavior will change:
{{ 12.50000 | floatformat:2 }}it will be displayed as12.50.{{ 34.00000 | floatformat:2 }}it will be displayed as34.00.{{ 5.678 | floatformat:2 }}it will be displayed as5.68(It will still round off here).
Through this parameter, we can flexibly control the display precision and method of decimal places according to the actual display requirements of the website. If the parameter is set to0,floatformatWill round the number to the nearest integer.
Actual application example
Suppose we have a variable in the templateproduct_price, its value may be29.99000/150.00000or7.345.
Remove extra zeros and automatically adjust the number of decimal places (default behavior):
<p>商品A价格:{{ product_price_A | floatformat }}</p> {# product_price_A = 29.99000,输出:29.99 #} <p>商品B价格:{{ product_price_B | floatformat }}</p> {# product_price_B = 150.00000,输出:150 #} <p>商品C价格:{{ product_price_C | floatformat }}</p> {# product_price_C = 7.345,输出:7.3 #}Force to keep two decimal places (even if it is zero, it will be displayed):
<p>商品A价格:{{ product_price_A | floatformat:2 }}</p> {# product_price_A = 29.99000,输出:29.99 #} <p>商品B价格:{{ product_price_B | floatformat:2 }}</p> {# product_price_B = 150.00000,输出:150.00 #} <p>商品C价格:{{ product_price_C | floatformat:2 }}</p> {# product_price_C = 7.345,输出:7.35 #}Round to the nearest integer:
<p>商品A价格:{{ product_price_A | floatformat:0 }}</p> {# product_price_A = 29.99000,输出:30 #} <p>商品B价格:{{ product_price_B | floatformat:0 }}</p> {# product_price_B = 150.00000,输出:150 #} <p>商品C价格:{{ product_price_C | floatformat:0 }}</p> {# product_price_C = 7.345,输出:7 #}
In summary, the Anqi CMS'sfloatformatA filter is a very practical template tool that helps us elegantly handle the display issues of floating-point numbers.尤其是默认的“remove unnecessary zeros”功能,让数字输出更符合日常阅读习惯,提升了网站内容的专业度和用户体验。