In the Aanqi CMS, the flexibility and accuracy of content display are one of the most valued features by users.When dealing with content related to numbers, especially when it involves amounts, percentages, or other data that requires precise display, the formatting of floating-point numbers is particularly important.34.23234or12.00000It may appear disorganized on the front-end page, affecting user experience and the professionalism of information.
It is fortunate that AnQi CMS is built-in with a powerful template engine, which draws on the essence of Django template syntax and provides a rich set of filters to help us fine-tune data processing.floatformatFilter is a powerful tool specifically used for floating-point number formatting, which allows your numbers to be displayed neatly and standardized, especially when it is necessary to retain a specific number of decimal places, its role is indispensable.
KnowfloatformatFilter
floatformatThe main function of the filter is to format floating-point numbers in a specified way. When you output a floating-point number variable directly in the template, if it has too many decimal places or is scattered, then throughfloatformatFilter, and you can easily unify their display format.
Its basic usage is very intuitive:{{ 您的浮点数变量 | floatformat:N }}
Here are theNRepresents the number of decimal places you want to keep. If omittedN,floatformatThe filter will use its default behavior for formatting.
How to accurately retain two decimal places?
You may often encounter the need to round off floating-point numbers to two decimal places, which is particularly crucial when displaying product prices, discounts, or monetary amounts. To achieve this goal, we just need to infloatformatFilter followed by:2.
For example, the price of a product is19.998and you want it to display as20.00; the price of another product is12.5and you want it to display as12.50; or an integer price100,English显示为100.00。这些都可以通过English实现。floatformat:2轻松实现English。
To be specific,{{ 您的浮点数变量 | floatformat:2 }}会执行以下操作:
- Round off:它会根据第三位小数的值,对第二位小数进行四舍五入English。
- 补齐小数位:如果原始数字的小数位数不足两位,它会自动在末尾补零,直到达到两位小数。
- 处理整数If the original number is an integer, it will convert it to a floating-point number with two decimal places.
This greatly ensures the consistency and aesthetics of the digital display, making your website content look more professional and easy to read.
Deeply Understandfloatformatflexibility
floatformatThe function of the filter is not limited to retaining two decimal places. We can adjustNto meet a wider range of needs:
Default behaviorWhen you only use
{{ 变量 | floatformat }}and do not specifyNWhen, the filter will retain one decimal place. But there is an exception: if the decimal part of the original number is0, it will be displayed as an integer without any decimal.{{ 34.23234 | floatformat }}will be displayed34.2{{ 34.00000 | floatformat }}will be displayed34{{ 34.26000 | floatformat }}will be displayed34.3(English: Note that it is rounded to one decimal place)
Retain more decimal placesIf you need to retain three, four, or even more decimal places, just
Nreplace it with the corresponding number. For example{{ 变量 | floatformat:3 }}This will keep three decimal places.{{ 34.23234 | floatformat:3 }}will be displayed34.232{{ 34.00000 | floatformat:3 }}will be displayed34.000
Negative numbers as parameters:
floatformatEven supports negative numbers as parameters, which will round the integer part of the number. For example,{{ 39.56000 | floatformat:"0" }}Rounds a number to the nearest integer and displays it as40.{{ 34.23234 | floatformat:"-3" }}意味着将数字四舍五入到千位,如果数字小于1000,则行为可能更复杂,但通常在不需要小数时负数参数较少使用。Supports a variety of input types: Whether it is a floating-point variable or a number stored as a string,
floatformatit can be handled very well, which provides great convenience for template development.
Actual code example
Let us illustrate with several specific examplesfloatformatApplication when retaining two decimal places:
{# 假设我们有一些浮点数变量 #}
{% set price_raw = 19.998 %}
{% set quantity_decimal = 12.5 %}
{% set integer_value = 100 %}
{% set zero_decimal = 5.00000 %}
{% set long_decimal = 3.1415926 %}
{% set string_number = "67.89123" %}
<h3>浮点数格式化展示</h3>
<p>原始价格 ({{ price_raw }}):{{ price_raw | floatformat:2 }}</p>
{# 预期输出: 原始价格 (19.998): 20.00 #}
<p>原始数量 ({{ quantity_decimal }}):{{ quantity_decimal | floatformat:2 }}</p>
{# 预期输出: 原始数量 (12.5): 12.50 #}
<p>整数值 ({{ integer_value }}):{{ integer_value | floatformat:2 }}</p>
{# 预期输出: 整数值 (100): 100.00 #}
<p>精确零小数 ({{ zero_decimal }}):{{ zero_decimal | floatformat:2 }}</p>
{# 预期输出: 精确零小数 (5.00000): 5.00 #}
<p>长小数位 ({{ long_decimal }}):{{ long_decimal | floatformat:2 }}</p>
{# 预期输出: 长小数位 (3.1415926): 3.14 #}
<p>字符串数字 ({{ string_number }}):{{ string_number | floatformat:2 }}</p>
{# 预期输出: 字符串数字 (67.89123): 67.89 #}
{# 默认 floatformat 行为 (不指定小数位) #}
<p>默认格式化 ({{ long_decimal }}):{{ long_decimal | floatformat }}</p>
{# 预期输出: 默认格式化 (3.1415926): 3.1 #}
<p>默认格式化 ({{ zero_decimal }}):{{ zero_decimal | floatformat }}</p>
{# 预期输出: 默认格式化 (5.00000): 5 #}
Through the above examples, you can clearly seefloatformat:2How to uniformly format various forms of numbers into two decimal places, greatly enhancing the professionalism and readability of the page content. In Anqi CMS, the reasonable use offloatformatFilter, it will make your website more superior in data display.
Common Questions (FAQ)
1.floatformatHow does the filter round off?
floatformatThe filter follows the standard mathematical rounding rules. For example, if you usefloatformat:2It will check the third decimal place. If the third decimal place is greater than or equal to 5, the second decimal place will be carried up; if it is less than 5, it will remain unchanged.
2. If I want to keep two decimal places, but the number itself is an integer (for example100),it will display as100.00?
Yes. When you explicitly specifyfloatformat:2when, even if the original number is an integer, it will be automatically