In a content management system, clear presentation and precise control of data are crucial, especially when displaying amounts, percentages, or any floating-point numbers. AnQiCMS (AnQi Content Management System) provides a powerful template engine, whichfloatformatFilter is a powerful tool to handle such floating-point number display issues.It can help us flexibly control the number of decimal places and implement different rounding rules according to the requirements, making the numerical data on the website both beautiful and accurate.

Core Function Analysis:floatformatFilter Overview

floatformatIt is a very practical filter in the AnQi CMS template, mainly used for formatting floating-point numbers to control the number of decimal places displayed. Whether you want to display product prices, statistics percentages, or any other decimal numbers,floatformatIt can present data in a unified and professional format to the user.

Its basic usage is very simple, through the pipe symbol|Pass the variable to be processed tofloatformatFilter, and you can optionally provide a parameter to specify the number of decimal places.

{{ 您的数值 | floatformat: 小数位数 }}

Fine-grained control: specifying the number of decimal places and the default behavior

floatformatThe filter shows flexible and diverse behavior when processing floating-point numbers, which mainly depends on whether you provide parameters and the specific values of the parameters.

Firstly,When used without any parametersfloatformatHourThe filter will intelligently try to keep one decimal place for floating-point numbers.However, if the decimal part of the calculated result is exactly zero (for example, 34.000), it will omit the decimal point and display it as an integer (such as 34).floatformatauto

  • For example:{{ 34.23234 | floatformat }}The result is34.2
  • For example:{{ 34.00000 | floatformat }}The result is34
  • For example:{{ 34.26000 | floatformat }}The result is34.3

Secondly,auto,floatformatIt will force the floating-point number to be formatted to the specified number of digits.Even if the decimal places of the original number are insufficient, it will be filled with zeros to reach the required number of places; if the decimal places of the original number exceed, it will be truncated according to the rounding rule.

  • For example:{{ 34.23234 | floatformat:3 }}The result is34.232
  • For example:{{ 34.00000 | floatformat:3 }}The result is34.000

auto

floatformatThe strength of the filter also lies in its ability to handle more complex rounding requirements.

When the parameter is0Hour,floatformatWill round the floating-point number to the nearest integer.It will strictly follow the standard rounding rules and display as an integer without any decimal points.This is very useful for scenarios where you need to display integer values in reports or summaries while also maintaining a certain rounding accuracy.

  • For example:{{ 39.56000 | floatformat:"0" }}The result is40(Rounding off)
  • For example:{{ 34.23234 | floatformat:"0" }}The result is34

When the parameter is a negative integer,floatformatProvided a unique way to control the 'number of significant digits'. For example,floatformat:"-N"will retainN位有效小数的同时,智能地移除末尾的零。这意味着它更侧重于浮点数的实际数值表示,避免不必要的零填充,使数字看起来更简洁。

  • For example:{{ 34.23234 | floatformat:"-3" }}The result is34.232
  • For example:{{ 34.00000 | floatformat:"-3" }}The result is34
  • For example:{{ 34.26000 | floatformat:"-3" }}The result is34.26

The main difference in usage of this negative integer parameter is that positive integer parameters willalwayspad with zeros to reach the specified number of digits, while negative integer parameters willRemoveUnnecessary trailing zeros.

Example of application scenarios: Make data display more professional

UnderstoodfloatformatAfter understanding the various uses, we can apply it in various scenarios to make data presentation more in line with expectations and user habits:

  1. Product price display:In e-commerce websites, prices usually need to be accurate to two decimal places.
    
    <p>商品价格: ¥ {{ product.Price | floatformat:2 }}</p>
    
  2. Percentage calculation and display:On the statistics or analysis page, convert ratios to percentages and retain an appropriate number of decimal places.
    
    {% set rate = 0.12345 %}
    <p>转化率: {{ (rate * 100) | floatformat:2 }}%</p> {# 结果是 12.35% #}
    
  3. Financial statement data:When you need to round large floating-point numbers to integers for an overview.
    
    <p>总销售额(约): {{ total_sales | floatformat:0 }} 元</p>
    
  4. Scientific or engineering data:Need to display significant digits while avoiding too many unnecessary zeros.
    
    <p>测量值: {{ sensor_data | floatformat:"-4" }} 单位</p>
    

Precautions

When usingfloatformatFiltering, there are several points to note:

  • Data Type: floatformat主要用于处理数字类型或可被解析为数字的字符串。If a non-numeric value is passed, it usually does not cause the template to break, but the result may not be what you expect.