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.AnQi CMS understands this need and provides a powerful and flexible floating-point number formatting function in the template engine, allowing you to easily control the display accuracy of floating-point numbers.

Next, we will explore two main methods of formatting floating-point numbers and retaining specified decimal places in AnQiCMS templates:floatformatFilters andstringformatfilter.

UsefloatformatFilter performs floating-point number formatting

floatformatThe filter is a convenient tool used specifically for floating-point number formatting in the AnQiCMS template.Its main function is to round off floating-point numbers according to the specified requirements and control the decimal places.

Default usage:

If no parameters are specified,floatformatThe filter attempts to keep floating-point numbers to one decimal place and intelligently remove trailing zeros. This means that like34.0Such numbers will be displayed as34while34.23234It will 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:

When you need to control the number of decimal places accurately, you canfloatformatAdd a number 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 filled out to the specified number of places.

`twig Set discount rate to 0.8 Set score to 98.7654 Set total amount to 99.9

Discount: {{ discountRate | floatformat:2 }}<p>