Website content operation is in progress, it is crucial to display numbers accurately, especially floating-point numbers such as prices and statistics, which are essential for enhancing user trust and the professionalism of the page.AnQi CMS knows this, and its powerful template engine provides simple and flexible tools, allowing content creators to easily control the display format of floating-point numbers, including decimal places, without delving into complex programming.
Next, we will discuss how to elegantly format floating-point numbers using built-in filters in Anqi CMS templates.
UsefloatformatFilters perform quick formatting.
Imagine, your website needs to display product prices or statistics, sometimes integers, sometimes with one or two decimal places.If it can be automatically handled, keeping the page tidy would be great.AnQi CMS'sfloatformatThe filter is designed for this purpose, it provides a very intuitive way to control the display of floating-point numbers.
The basic usage is:{{ 您的浮点数变量 | floatformat }}or{{ 您的浮点数变量 | floatformat:位数 }}
If you do not specify位数parameter,floatformatWill intelligently round off floating-point numbers to one decimal place. A very practical feature is that if the last digit after rounding off is zero, it will automatically omit the decimal point and zero, making the display more concise. For example,{{ 34.23234 | floatformat }}It will display34.2while{{ 34.00000 | floatformat }}It displays simply34. If the result is34.26, it will automatically round off the display.34.3.
When you need to display a fixed number of decimal places, just add the required number of digits after the colon. For example,{{ 34.23234 | floatformat:3 }}it will display accurately34.232.floatformatIt will automatically perform standard rounding to ensure that the displayed data conforms to conventional numerical habits. You can even force it to display as an integer, such as位数is set to0to force it to display as an integer, for example{{ 39.56000 | floatformat:"0" }}you will get40.
This filter performs very well in scenarios where quick, intelligent control of decimal places is needed, and it is desired that the number automatically omits zeros at the end.
UtilizestringformatThe filter achieves precise control
If we sayfloatformatIt is a convenient tool for daily use, thenstringformatIt is a tool for fine-tuning. It provides a closer Go language formatting at the lower levelfmt.SprintfThe ability, allowing you to output in a C language style format string, with precise control over output, including decimal places, leading zeros, and more.
The basic usage is:{{ 您的浮点数变量 | stringformat:"格式字符串" }}
Among them,"格式字符串"Is critical. We usually use it for floating-point numbers."%.xf"In such a format,xRepresents the number of decimal places you want to keep. For example, to display two decimal places, even if the last digit is zero, it should be displayed, you can use"%.2f"Thus,{{ 3.1 | stringformat:"%.2f" }}It will be displayed as3.10while{{ 3.141592653 | stringformat:"%.2f" }}will be rounded to3.14.
stringformatIn need of strict control over output format, such as uniform currency display format (always retaining two decimal places), specific report data format, or the need to use Go languagefmt.Sprintfother advanced formatting features (such as leading zero for numbers) are especially powerful. It provides higher flexibility and precision.
how to choose the appropriate filter
In practice, choosefloatformatOrstringformatDepending on your specific needs:
- Select
floatformatat the right time:When you simply want to control the number of decimal places and accept the default rounding behavior (for example,34.0displayed as34,34.26displayed as34.3),and the number at the end of the decimal point and zero can be automatically omitted.floatformatIs a faster, more concise choice. - Select
stringformatat the right time:When you need more strict and precise formatting control, such as always displaying a fixed number of decimal places (3.1displayed as3.10), or need to use Go languagefmt.SprintfThe other advanced formatting features (such as number width alignment, scientific notation, etc.), thenstringformatwill be your preference.
ByfloatformatandstringformatThese powerful filters allow Anqi CMS users to flexibly beautify and standardize the display of numbers on websites, whether it is daily content publishing or complex report presentation, they can do it with ease, greatly improving the quality of content and user experience.
Frequently Asked Questions (FAQ)
- Q:
floatformatandstringformatWhat are the differences when dealing with rounding?A: They all follow the standard rounding rules. The main difference lies infloatformatWhen not specifying a fixed number of digits, it has an intelligent default behavior (omitting trailing zeros), for example34.00000It will be displayed as34while34.26000It will be displayed as34.3.stringformatIt will process strictly according to the format string you provide, for example"%.2f"It will always display two decimal places, even if3.0it will also be displayed as3.00. - Q: I want to add a currency symbol when displaying numbers, like “¥100.00”, how should I do it?A: You can combine
stringformatfiltering and string concatenation to achieve. For example, if your floating-point variable isprice, then you can write it like this:{{ "¥" }}{{ price | stringformat:"%.2f" }}This way ensures that the currency symbol is closely integrated with the formatted number. - Q: If my variable is not a floating point number, can these filters still be used?A:
floatformatThe primary expectation is to receive floating-point numbers or numeric strings that can be converted to floating-point numbers. If not, it may return unexpected results or errors.stringformatIt is more general, it can format any type of variable into a string, but make sure you provide"格式字符串"compatible with the variable type, for example%f