In website content operation, accurately displaying numbers, especially floating-point numbers, is often a key factor affecting user experience and data professionalism.Whether it is product prices, statistics, or scientific reports, it is crucial to ensure that numbers are presented in a consistent and easy-to-read format.floatformatFilter, allowing content creators to flexibly control the display precision of floating-point numbers.

floatformatFilter: Fine-grained control of floating-point number display.

floatformat

Its basic usage is to apply it to a number or a variable that can be converted to a number, for example{{ 你的变量 | floatformat }}.

Default behavior: Smart Simplified Display

When you usefloatformatThe filter will adopt an intelligent default strategy when no parameters are specified: it will round floating-point numbers to one decimal place, but if the decimal part is exactly.0It will directly remove the decimal point and the zeros after it for simplicity.

For example:

  • {{ 34.23234 | floatformat }}it will be displayed as34.2(Rounded to one decimal place)
  • {{ 34.00000 | floatformat }}it will be displayed as34(Remove redundant zeros)
  • {{ 34.26000 | floatformat }}it will be displayed as34.3(Rounded to one decimal place)

This default behavior is very convenient in many scenarios, especially when you want the number to be concise but still retain a certain precision.

Setting the number of positive digits: precise to a fixed decimal place

When your business requirements need to strictly display a fixed number of decimal places for floating-point numbers, you can do so by settingfloatformatThe filter provides a positive integer parameter to implement.This parameter specifies the number of decimal places to be retained after the decimal point.Even if the decimal part of the original number is not enough, it will be filled with zeros to ensure consistent display length.

For example, if you want all numbers to be precise to three decimal places:

  • {{ 34.23234 | floatformat:3 }}it will be displayed as34.232(Ensure precision to three decimal places)
  • {{ 34.00000 | floatformat:3 }}it will be displayed as34.000(Zero-filled to three decimal places)
  • {{ 34.26000 | floatformat:3 }}it will be displayed as34.260(Zero-filled to three decimal places)

This usage is very suitable for scenarios such as financial reports, scientific data presentation, etc., which require strict alignment and uniform precision.

Negative number digit settings: Flexible removal of extra zeros

Here isfloatformatThe more flexible side of the filter. When you providefloatformatA negative integer parameter for the filter, such asfloatformat:"-N"It will also round up the number toNauto, but unlike a positive number parameter, it will intelligently remove trailing zeros after formatting.This means that if a number can be expressed more concisely without losing precision, it will do so.

For example, if we still base it on keeping three decimal places but want to remove unnecessary trailing zeros:

  • {{ 34.23234 | floatformat:"-3" }}it will be displayed as34.232(Rounded to three decimal places, trailing zeros can be removed)
  • {{ 34.00000 | floatformat:"-3" }}it will be displayed as34(Trailing after rounding to three decimal places).000can be removed)
  • {{ 34.26000 | floatformat:"-3" }}it will be displayed as34.260(Trailing after rounding to three decimal places).0is retained because the original precision is required)

It can be seen through comparison that when trailing zeros have no actual meaning, negative parameters can make the number display cleaner while still maintaining the highest precision you specified.This way can make data both precise and beautiful in many cases.

It is worth noting that,floatformatThe filter is also very intelligent, even if your data exists in the form of floating-point numbers as strings (for example"34.23234"),it can also perform the conversion and formatting correctly, which provides more convenience for data processing.

Summary

floatformatfloatformatwill make your website content more refined and accurate.


Common Questions (FAQ)

  1. **问:`floatformat' in English