When using Anqi CMS for website content management, we often encounter scenarios where precise control of numbers is required, especially when it comes to floating-point numbers such as prices and statistical data, where rounding up or down is particularly important.The AnQi CMS template engine provides flexible filters that can help us easily meet these requirements.There is no direct 'round up' or 'round down' function label, but by reasonably utilizing existing functions, we can still achieve the purpose.

1. UtilizefloatformatFilter implementation of 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,floatformatThe filter is our first choice. This filter can very conveniently control the display accuracy of numbers.

The basic usage is{{ 变量 | floatformat:位数 }}.

Example usage:

  • Default rounding to one decimal place (if the decimal part is 0, it will not be displayed):If we have a number3.14159and we want to round it to one decimal place, it can be written like this:{{ 3.14159 | floatformat }}The output will be3.1.34.00000,{{ 34.00000 | floatformat }}It will output.34.34.26000,{{ 34.26000 | floatformat }}It will output.34.3(Because the second digit is 6, which is greater than or equal to 5, so it rounds up).

  • Specify the number of decimal places to be retained:If we need to retain two decimal places, we can specify it like this.floatformat:2:{{ 3.14159 | floatformat:2 }}The output will be3.14.{{ 3.148 | floatformat:2 }}The output will be3.15.

  • Use a negative number to round to the nearest integer: floatformatThe filter also supports using negative numbers to specify rounding to integer places, similar to what we commonly refer to as 'rounding to tens', 'rounding to hundreds', and so on. Negative number-NRepresents rounding to the first digit on the left of the decimal pointN.{{ 34.26000 | floatformat:-1 }}Will round the number to the nearest unit, output34.{{ 34.76000 | floatformat:-1 }}It will output.35.{{ 123.45 | floatformat:-2 }}Will round the number to the nearest ten, output120.

ByfloatformatFilter, we can flexibly handle the display precision of floating-point numbers to ensure that data is presented in a user-friendly manner.

Second, usingintegerFilter implements truncation or truncation.

In some cases, we may need to truncate a floating-point number directly, or simply truncate its decimal part. At this time,integera filter can come into play.integerThe filter's role is to convert a value to an integer. For floating-point numbers, it will truncate the decimal part directly, retaining only the integer part.

The basic usage is{{ 变量 | integer }}.

Example usage:

  • Round down positive numbers (truncate decimals): {{ 5.7 | integer }}The output will be5.{{ 5.3 | integer }}The output will be5. This behavior is exactly equivalent to the mathematical 'floor' for positive numbers.

  • Truncate decimals 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 mathematical 'floor' (mathematicalfloor(-5.7)Is-6),but is equivalent to the ceiling function in mathematics for negative numbers. In other words, for negative numbers,integerThe filter actually has the effect of

Therefore, if your requirement is to round down for positive numbers or round up for negative numbers,integerThe filter can meet it directly.

3. Simulate ceiling (Ceiling)

The AnQi CMS template does not have a direct 'Ceiling' filter, but we can achieve it byintegerThe core idea is to simulate the implementation of filters and conditional judgments. If a positive number has a fractional part, its ceiling result is the 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 ceiling:

`twig {% set myNumber = 10.3 %} {% set integerPart = myNumber | integer %} {% if myNumber > integerPart %}

{% set ceilResult = integerPart + 1 %}

{% else %}

{% set ceilResult = integerPart %}

{% endif %} {{ myNumber }} 向上取整为: {{ ceilResult }} {# Output: 10.3 Rounding up to: 11 #}

{% set myNumber = 10.0 %} {% set integerPart = myNumber | integer %} {% if