In AnQiCMS templates, when we need to display floating-point numbers, we sometimes encounter them appearing in scientific notation, like1.23E+07Instead12,300,000.This display method may be useful in some specific scenarios, but it is often difficult for ordinary users to understand intuitively, affecting the aesthetics and readability of the page. This is especially true in situations where displaying product prices, statistics, and other information that does not require high accuracy but does require high intuitiveness.

AnQiCMS's template engine design borrows from Django syntax, which provides us with a flexible and powerful tool for handling floating-point number display in templates—template filters.By reasonably utilizing these filters, we can easily convert scientific notation to a more readable conventional floating-point number format.

UsefloatformatFilter controls the display of floating-point numbers

AnQiCMS provides a namedfloatformatThe filter 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 directly on a floating-point number variable,floatformatthe filter will intelligently process it according to the magnitude of the number:

{{ 商品价格|floatformat }}

For example,商品价格has a value of12345.6789By default,floatformatit may display it as12345.7[If the original value is precise to one decimal place) or12345.68If the original value is accurate to two decimal places, it will attempt to display it as simply as possible according to the characteristics of the number, while avoiding scientific notation.If the number is too large or too small, it usually retains only a few decimal places and is 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 }}

Therefore, regardless of商品价格Yes12345.6789Or12345.0, it will be formatted as12345.68or12345.00Even if the number is very large, like1234567890.123Use{{ 价格|floatformat:2 }}it will be displayed as1234567890.12, without scientific notation.

If your floating-point number may not contain a fractional part, but you still want to display a decimal point uniformly (for example, currency display)100.00Instead100)floatformatAlso handles it well. By specifying the number of decimal places, it automatically fills in the trailing zeros.

With the help ofstringformatThe filter implements more flexible formatting

Exceptfloatformat, AnQiCMS also provides more powerful functionsstringformatFilter, it is similar to a programming language in that.sprintfFunction, which can output strings in a more complex format.floatformatIt is enough to solve most problems of avoiding scientific notation.stringformatIt has more advantages when it needs to combine other text or perform more precise numerical alignment.

To avoid scientific notation, we can instringformatin%fFormat specifiers. Combined with precision control, for example%.2fRepresents a floating-point number with two decimal places:)

{{ 统计数据|stringformat:"%.2f" }}

Here%.2fIt will统计数据Formatted as a floating-point number with two decimal places, even if the original value is)1.234E+07, it will be displayed as12340000.00.

stringformatThe advantage lies in its versatility, allowing you to 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.'

Recommended for actual application

  1. Are you sure you need to handle the variable?Identify all possible floating-point variables in your template, especially those from database or numeric fields from user input likearchive.Price/archive.Stockor custom model fields.
  2. Select an appropriate filter: For simple floating-point display and rounding,floatformatFilters are usually the preferred choice, as they are simple and easy to use. If you need to embed numbers into complex text or require more advanced formatting controls (such as width padding, left/right alignment, etc.),stringformatProvided greater flexibility.
  3. Consider default values.: If some numerical variables may be empty or not exist, you may need to combinedefaultA filter to provide a friendly placeholder, avoiding empty values or errors on the page. For example:
    
    {{ 商品价格|floatformat:2|default:"暂无报价" }}
    
  4. Maintain consistencyThroughout the entire website, for numerical data of the same type (such as all product prices), try to use a consistent formatting strategy to provide a unified user experience.

Through the above method, you can effectively avoid floating-point numbers from being displayed in scientific notation in the AnQiCMS template, making your website content more professional and easy to understand.

Common Questions (FAQ)

  1. Q:floatformatandstringformatWhich one is recommended to use to avoid scientific notation?A: For just controlling the number of decimal places of a floating-point number and avoiding 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 for formatting, or need to implement more complex alignment, padding, and other requirements,stringformatIt would be a more flexible choice.

  2. Q: Will using these filters change my original data?A: No. Template filters only affect the data on the front-end page.displayMethods, they will not modify the original data stored in the AnQiCMS backend database. No matter how you format, the value saved on the backend is always the precise original value.

  3. Q: If I want to display a very large integer, like123456789012345But it becomes scientific notation in the template, are these filters useful?A:floatformatandstringformatMainly used forfloating-point numbersIf a very large integer is recognized and converted to a floating-point number by the AnQiCMS template engine (for example, when it exceeds the Go language'sint64The representation range, or sometimes promoted to a floating-point number in certain calculations), 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 such a problem, it is usually because of implicit type conversion at some stage.