In website content operation, we often need to display floating-point number information such as prices, percentages, and ratings.The accuracy and display method of these numbers directly affect user experience and the professionalism of information.Aq CMS knows this need and provides a powerful and flexible floating-point number formatting feature in the template engine, allowing you to easily control the display accuracy of floating-point numbers.
Next, we will discuss the two main methods for formatting floating-point numbers and retaining the specified number of decimal places in AnQiCMS templates:floatformatfilters andstringformatFilter.
UsefloatformatFilter for floating-point number formatting
floatformatThe filter is a convenient tool used for floating-point formatting in the AnQiCMS template.Its main function is to round off floating-point numbers according to your specified requirements and control the number of decimal places.
Default usage:
If no parameters are specified,floatformatthe filter will attempt to keep one decimal place for floating-point numbers and intelligently remove trailing zeros. This means that numbers like34.0will be displayed as34while34.23234will be displayed as34.2.
{% set price = 123.4567 %}
{% set quantity = 5.0 %}
{% set rate = 34.26000 %}
<p>原价:{{ price | floatformat }}</p> {# 结果:123.5 #}
<p>数量:{{ quantity | floatformat }}</p> {# 结果:5 #}
<p>转换率:{{ rate | floatformat }}</p> {# 结果:34.3 #}
Specify the number of decimal places:
You can specify the number of decimal places when you need to control the precision.floatformatAdd a number at the end to indicate the number of decimal places you want to keep. In this case, even if the decimal part is zero, it will be automatically padded to the specified number of digits.
Discount: {{ discountRate | floatformat:2 }}