In AnQi CMS, the flexibility and accuracy of content display is one of the most valued features by users.When dealing with content related to numbers, especially involving amounts, percentages, or other data that requires precise display, the formatting of floating-point numbers is particularly important.An unprocessed floating-point number, for example34.23234or12.00000It may appear disorganized on the front-end page, affecting user experience and the professionalism of information.
幸运的是,the AnQi CMS comes with a powerful template engine, which draws on the essence of Django template syntax and provides a rich set of filters to help us refine data processing. Among them, floatformatThe filter is a tool specifically designed for floating-point number formatting, which can make your numbers display neatly and standardized, especially when it is necessary to retain specific decimal places, its role is indispensable.
Get to knowfloatformatFilter
floatformatThe main function of the filter is to format floating-point numbers in a specified way. When you directly output a floating-point variable in a template, if it has too many decimal places or is scattered, then throughfloatformatA filter can easily unify their display format
Its basic usage is very intuitive:{{ 您的浮点数变量 | floatformat:N }}
HereNRepresents the number of decimal places you want to retain. 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 important when displaying product prices, discounts, or monetary amounts. To achieve this goal, we just need tofloatformatadd after the filter:2Just do it.
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 price100and you want to display as100.00These can all be passed throughfloatformat:2It can be easily realized
To be specific,{{ 您的浮点数变量 | floatformat:2 }}It will perform the following operations:
- Round off: It will round off the second decimal place based on the value of the third decimal place.
- Round off decimal placesIf the decimal places of the original number are less than two, it will automatically add zeros at the end until it reaches two decimal places.
- Handle integersIf 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 readable.
Get to know in-depthfloatformatflexibility
floatformatThe function of the filter is not limited to retaining two decimal places. We can adjustNthe value to meet a wider range of needs:
Default behavior: When you only use
{{ 变量 | floatformat }}Not specifyingNWhen, the filter will keep 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 }}It will display34.2{{ 34.00000 | floatformat }}It will display34{{ 34.26000 | floatformat }}It will display34.3(Note that this is rounded to one decimal place)
Keep more decimal places: If you need to keep three, four, or even more decimal places, just replace
Nwith the corresponding number. For example{{ 变量 | floatformat:3 }}The value will be kept to three decimal places.{{ 34.23234 | floatformat:3 }}It will display34.232{{ 34.00000 | floatformat:3 }}It will display34.000
Negative numbers as parameters:
floatformatEven supports negative numbers as parameters, which will round from the integer part of the number. For example,{{ 39.56000 | floatformat:"0" }}rounds the number to the nearest whole integer and displays as40However{{ 34.23234 | floatformat:"-3" }}means rounding a number to the nearest thousand, and if the number is less than 1000, the behavior may be more complex, but usually the use of negative parameters is less frequent when decimals are not needed.Supports multiple input types: Whether it is a floating-point variable or a number stored as a string,
floatformatit can be handled well, which provides great convenience for template development.
Actual code example
Let's demonstrate 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 #}
You can clearly see from the above examplesfloatformat:2How to format numbers of various forms into two decimal places, which greatly improves the professionalism and readability of the page content. In Anqi CMS, it is used reasonablyfloatformatThe filter will make your website stand out in data presentation.
Frequently Asked Questions (FAQ)
1.floatformatHow does the filter perform rounding?
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, then the second decimal place will be rounded 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 though the original number is an integer, it will automatically