How to format numbers, strings, arrays, and any other values into a string with a specified format in the Anqi CMS template?
stringformat
The filter can format any value such as numbers, strings, arrays, etc. into strings and output them in a specified format.
Some formatting standards:
%v
Output structure {10 30}%+v
Output structure display field name {one:10 tow:30}%#v
Output structure source code snippet main.Point{one:10, tow:30}%T
Type of output value main.Point%t
Output formatted boolean true%d
Output standard decimal format 100%b
Output standard binary format 99 corresponding to 1100011%c
Output the corresponding characters of a fixed integer 99 corresponding c%x
Output hexadecimal code 99 corresponding to 63%f
Output decimal formatting 99 corresponding to 63%e
Output scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08%E
Output scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08%s
Perform basic string output “\“string\”” corresponds to “string”}]%q
The output with double quotes in the source code “\“string\”” corresponds to “\“string\””%p
Output the value of a pointer &jgt corresponding to 0xc00004a090%
The output width is controlled later. The default result is right-aligned and the blank part is filled with spaces.%2.2f
Specify the output width of the floating point type 1.2 corresponding to 1.20%*2.2f
Specify the output width alignment of the floating point type, using-
Flag 1.2 corresponds to *1.20
How to use
stringformat
How to use filters:
{{ obj|stringformat:"格式定义" }}
For example3.141592653
Format the number to keep 2 decimal places, it can be written like this:
{{ 3.141592653|stringformat:"%.2f" }}
# 显示结果
3.14
Sample Demo
{{ simple.float|stringformat:"%.2f" }}
{{ simple.uint|stringformat:"Test: %d" }}
{{ simple.chinese_hello_world|stringformat:"Chinese: %s" }}
# 显示结果
Hello!
3.14
Test: 8
Chinese: 你好世界