When using AnQi CMS for website content management, we often encounter scenarios where precise control over numbers is required, especially when it comes to floating-point numbers such as prices and statistics, where rounding, ceiling, or flooring operations are particularly important.The template engine of AnQi CMS provides flexible filters that can help us easily meet these requirements.Although there is no direct 'ceiling' or 'floor' function tag, we can still achieve the purpose by making good use of existing functions.
1. UtilizefloatformatFilter to implement floating-point number rounding
When we need to round a floating-point number according to standard rules and retain a specific number of decimal places,floatformatFilter is our first choice. This filter can control the display accuracy of numbers very conveniently.
Its basic usage is{{ 变量 | floatformat:位数 }}.
Example usage:
Round to one decimal place (if the decimal part is 0, it will not be displayed):If we have a number
3.14159And we want to round it to one decimal place, we can write it like this:{{ 3.14159 | floatformat }}The output will be:3.1If the number is34.00000,{{ 34.00000 | floatformat }}It will output34If the number is34.26000,{{ 34.26000 | floatformat }}It will output34.3(Because the second digit is 6, which is greater than or equal to 5, so it is rounded up).Specify the number of decimal places to retain:If we need to retain two decimal places, we can specify it like this
floatformat:2:{{ 3.14159 | floatformat:2 }}The output will be:3.14.{{ 3.148 | floatformat:2 }}The output will be:3.15.Using negative numbers to round to the nearest integer:
floatformatThe filter also supports using negative numbers to round to the nearest integer, similar to what we commonly refer to as 'rounding to the ten place', 'rounding to the hundred place', and so on. Negative numbers-NRounds to the left of the decimal point.Ndigit.{{ 34.26000 | floatformat:-1 }}Rounds the number to the nearest whole number, outputting.34.{{ 34.76000 | floatformat:-1 }}It will output35.{{ 123.45 | floatformat:-2 }}Rounds the number to the nearest ten, outputting.120.
PassfloatformatFilter, we can flexibly handle the display precision of floating-point numbers to ensure that data is presented in a user-friendly manner.
Secondly, utilizingintegerFilter implementation for truncation or rounding down
In some cases, we may need to truncate a floating-point number directly, or simply truncate its decimal part.integerthe filter can come into play.integerThe function of the filter is to convert a value to an integer. For floating-point numbers, it will truncate the decimal part directly, only keeping the integer part.
Its basic usage is{{ 变量 | integer }}.
Example usage:
Floor a positive number (truncate the decimal):
{{ 5.7 | integer }}The output will be:5.{{ 5.3 | integer }}The output will be:5This behavior is exactly equivalent to the mathematical 'floor' operation for positive numbers.Truncate to zero for negative numbers:For negative numbers,
integerThe filter truncates decimals towards zero.{{ -5.7 | integer }}The output will be:-5.{{ -5.3 | integer }}The output will be:-5. This is slightly different from the 'Floor' operation in mathematics (floor(-5.7)Yes-6),but is equivalent to the mathematical 'ceiling' operation for negative numbers. In other words, for negative numbers,integerThe filter actually has the effect of 'rounding up', making it closer to zero.
Therefore, if your requirement is to round down for positive numbers or round up for negative numbers,integerFilter can meet directly.
3. Ceiling simulation
The AnQi CMS template does not have a direct "Ceiling" filter, but we can achieve it byintegerUsing filters and conditional judgments to simulate implementation.The core idea is: if a positive number has a fractional part, its ceiling result is its integer part plus one; if it is an integer itself, it remains unchanged.
Example usage:
Suppose we want to round up a positive numbermyNumberto the nearest integer:
auto
{% set ceilResult = integerPart + 1 %}
{% else %} English
{% set ceilResult = integerPart %}
{% endif %} {{ myNumber }} 向上取整为: {{ ceilResult }} {# Output: 10.3 向上取整为: 11 #}
{% set myNumber = 10.0 %} {% set integerPart = myNumber | integer %} {% if %}