In website operation, the way data is displayed is crucial to user experience.Whether it is product prices, statistics, or financial statements, a clear and standardized numerical format can not only enhance the professionalism of the website but also help users understand information more intuitively.The AnQi CMS is aware of this need and provides powerful number formatting features in the template system, allowing us to easily control the decimal places of floating-point numbers, the display format of integers, and more customized number presentations.

Why is number formatting so important?

Imagine, a product price on an e-commerce website is displayed as '199.9999 yuan' or '$20.00000', which undoubtedly will bring confusion to users.Similarly, if the data is displayed as “1234567”, but we want it to be presented in the form of “1,234,567”, the lack of formatting can make the data difficult to read.The digital formatting function of AnQi CMS is exactly to solve these problems, help us finely control the display effect of every number on the website, making the data more in line with actual business scenarios and user habits.

Flexibly control decimal places: floatformat filter

When we need to control the decimal places of floating-point numbers, for example, to uniformly display currency prices or percentages,floatformatThe filter is the preferred tool provided by the security CMS. This filter is specifically designed to handle floating-point numbers and can retain the specified number of decimal places according to our needs.

Its basic usage is to add it after the variable that needs to be formatted|floatformat:Nwhere,NRepresents the number of decimal places you want to keep

For example, suppose your product price variableproductPricehas a value of34.23234:

  • Default behavior:If you do not specify the number of decimal places, that is{{ productPrice|floatformat }}It will try to retain one decimal place, and if the last digit is zero, it will directly remove the decimal part, for example34.2or34. If your product price is34.00000,显示出来就会是English34;如果是English34.26000,则会被四舍五入为English34.3.

  • Specify the number of decimal places:当您希望精确控制小数位数时,可以提供一个正整数作为参数。比如,English{{ productPrice|floatformat:3 }}It will34.23234格式化为English34.232while34.00000will be displayed as34.000This is very useful when displaying financial data that requires fixed decimal places.

floatformatThe filter is very intelligent, it can correctly identify and format your variables whether they are direct numeric types or numeric strings.

Meet advanced formatting requirements: stringformat filter

floatformatThe filter performs well in controlling the decimal places of floating-point numbers, but if you have broader needs for number formatting, such as inserting numbers into specific text templates, or need to follow C languageprintfThe formatting rules of the style, thenstringformatThe filter will be your first choice.

stringformatThe filter provides similar to the Go languagefmt.Sprintf()The powerful function allows you to output any value (including numbers, strings, etc.) in the format of a string that you define.

The syntax for its use is{{ 变量名|stringformat:"格式定义" }}。where 'Format definition' is a string that includes various formatting placeholders.

Common format definitions for numbers include:

  • %dis used for formatting integers. For example,{{ someInteger|stringformat:"%d" }}auto English888auto English888.
  • %fauto English{{ someFloat|stringformat:"%f" }}It will3.141592653auto English3.141593.
  • %.Nfauto EnglishNauto EnglishfloatformatThe effect is similar, but provides finer control. For example,{{ someFloat|stringformat:"%.2f" }}It will3.141592653auto English3.14.

stringformatThe strength lies in its ability to mix and format numbers with other text. For example, if you want to display a number as 'Test Result: 888', you can use it like this:{{ someInteger|stringformat:"测试结果:%d" }}This makes it very flexible when creating dynamic text.

Combined usage: preprocessing and formatting

Sometimes, the data you retrieve from the backend may not be of standard numeric type, but exist in string format. In this case, SafeCMS also providesintegerandfloatFilter used to convert these strings to their corresponding numeric types before formatting.

For example, if yourproductPricevariable is initially a string"34.23234", you can use it first tofloatThe filter converts it to a floating-point number, thenfloatformatPerform decimal place control:

{# 假设 productPrice 变量为字符串 "34.23234" #}
{{ productPrice|float|floatformat:2 }}

Similarly, if a string"123"needs to be confirmed as an integer for subsequent operations,{{ "123"|integer }}the conversion can be completed.

Summary

Secure CMS throughfloatformatandstringformatThese powerful filters provide us with flexible and precise numeric formatting capabilities.Whether it is to simplify decimal places of floating-point numbers or to integrate numbers into complex text outputs, we can always find the right tools.These finely detailed features help us build more professional and user-friendly websites, presenting data in the clearest and most expected form to visitors.


Common Questions (FAQ)

  1. Q:floatformatandstringformatWhat are the main differences in number formatting between filters?A:floatformatThe filter is mainly used for retaining and rounding specific decimal places of floating-point numbers, especially suitable for quickly adjusting the decimal display of numbers, and its default behavior will remove the trailing zeros.stringformatFilter is more general, it allows you to use various placeholders (such asprintflike functions, using various placeholders (such as%d/%f/%.2f)来定义数字或任何其他类型值的输出字符串格式,更适合需要将数字嵌入到复杂文本或进行多类型格式化转换的场景。

  2. Q: If the number I get from the background is a text string (for example, “123.45”), can I use it directly?floatformat?A: Yes, you can.floatformatThe filter is smart enough to recognize numbers in string format and convert them to floating-point numbers for formatting. However, to ensure the accuracy and robustness of the data, you can also choose to use it first if you are sure that the variable is a number but exists in string format.floatFilter explicit conversion, for example{{ "123.45"|float|floatformat:2 }}.

  3. Q: I see another one in the documentstampToDateDoes the label have anything to do with number formatting??A:stampToDateTags and number formatting differ, it is specifically used to convert timestamps (usually an integer or a long integer) to a readable date and time format. For example,{{ stampToDate(1678886400, "2006-01-02 15:04") }}The timestamp will be formatted as “2023-03-15 15:04”. Although the timestamp itself is a number,stampToDateThe purpose is to present it as a date-time string rather than a simple numeric format adjustment.