In AnQiCMS template, when we need to display floating-point numbers, we sometimes encounter them appearing in scientific notation, such as1.23E+07instead of12,300,000. This display method is useful in some specific scenarios, but it is often difficult for ordinary users to understand intuitively, affecting the aesthetics and readability of the page, especially in cases where the accuracy is not required but the intuitiveness is, such as displaying product prices, statistical data, and other situations.
AnQiCMS's template engine design borrowed the Django syntax, which provides us with a flexible and powerful tool for handling floating-point number display in templates - the template filter.By reasonably using these filters, we can easily convert the scientific notation into a more readable regular floating-point number format.
UsefloatformatFilter controls the display of floating-point numbers
AnQiCMS provides a namedfloatformatA filter specifically used for formatting floating-point numbers. Its main function is to control the decimal places of floating-point numbers and automatically avoid the display of scientific notation.
When we use a filter directly on a floating-point variablefloatformatit will intelligently process the size of the numbers:
{{ 商品价格|floatformat }}
For example, if商品价格The value is12345.6789By default,floatformatit may display it as12345.7(If the original value is accurate to one decimal place) or12345.68If the original value is accurate to two decimal places, it will try to display it as simply as possible while avoiding scientific notation.If a number is too large or too small, it will usually retain a few decimal places and then be displayed in a conventional form.
If you need to control the number of decimal places more precisely, you canfloatformatThe filter passes an integer parameter representing the number of decimal places to retain. For example, to always retain two decimal places:
{{ 商品价格|floatformat:2 }}
So, no matter商品价格Is12345.6789Or12345.0will be formatted as:12345.68or12345.00. Even if the number is very large, such as1234567890.123, using{{ 价格|floatformat:2 }}it will also be displayed as1234567890.12It will not appear in scientific notation.
If your floating-point number may not contain a decimal part, but you still want to display a decimal point uniformly (for example, for currency display100.00instead of100)floatformatIt can also cope well. By specifying the number of decimal places, it will automatically fill in the trailing zeros.
WithstringformatThe filter implements more flexible formatting.
exceptfloatformat, AnQiCMS also provides more powerful functions.stringformatA filter, it is similar to a function in programming languages,sprintfwhich can define output strings in more complex formats. Althoughfloatformatit is sufficient to solve most problems avoiding scientific notation, butstringformatIt has an advantage when it is necessary to combine other text or to align numbers more finely.
To avoid scientific notation, we can usestringformatis used%fformat specifiers. Combined with precision control, for example%.2fIndicates a floating-point number rounded to two decimal places:
{{ 统计数据|stringformat:"%.2f" }}
Here%.2fWill统计数据Formatted as a floating-point number with two decimal places, even if the original value is1.234E+07, it will also be displayed as12340000.00.
stringformatThe advantage lies in its versatility, you can embed numbers into more complex strings and precisely control their format:
本次交易金额为:{{ 交易金额|stringformat:"¥ %.2f" }} 元
This will output results similar to 'The transaction amount is: ¥ 1234.56 yuan.'.
Actual application suggestions
- Confirm the variable needs to be processed.In your template, identify all possible variables that output floating-point numbers, especially those from database or user input numeric fields such as
archive.Price/archive.Stockor custom model fields containing numbers. - Choose the appropriate filterFor simple floating-point display and rounding,
floatformatThe filter is usually the preferred option because it is concise and easy to use. If you need to embed numbers into complex text or require more advanced formatting control (such as width padding, left/right alignment, etc.),stringformatProvided greater flexibility. - Consider the default value.: If some numerical variables may be empty or non-existent, you may need to combine
defaultA filter is used to provide a friendly placeholder to avoid displaying empty values or errors on the page. For example:{{ 商品价格|floatformat:2|default:"暂无报价" }} - Maintain consistencyIn the entire website, for the same type of numerical data (such as all product prices), try to use a consistent formatting strategy to provide a unified user experience.
By using the above method, you can effectively avoid displaying floating-point numbers in scientific notation in AnQiCMS templates, making your website content more professional and easy to understand.
Frequently Asked Questions (FAQ)
Q:
floatformatandstringformatWhich one is recommended to avoid scientific notation?A: For those who just want to control the decimal places of floating-point numbers and avoid scientific notation,floatformatMore direct and recommended, as it is more semantically clear and easier to use. If you need to mix numbers with other text formatting, or need to implement more complex alignment, padding, and other requirements, thenstringformatWould be a more flexible choice.Q: Would using these filters change my original data?A: No. Template filters only affect the data on the frontend page.DisplayMethods, they will not modify the original data stored in the AnQiCMS backend database. No matter how you format, the values saved in the backend are always the precise original values.
Q: If I want to display a very large integer, such as
123456789012345But it turned into scientific notation in the template, are these filters useful?A:floatformatandstringformatMainly used for processingFloating point numbers. If a very large integer is recognized by the AnQiCMS template engine and converted to a floating-point number type (for example, when it exceeds the Go languageint64The representation range, or sometimes promoted to a floating-point number), and when displayed in scientific notation, these filters can convert them back to the conventional floating-point number representation.But if it is always treated as an integer, it is usually not displayed in scientific notation.If you encounter this kind of problem, it is usually because the data type has undergone an implicit conversion at some stage.