How to format any value such as numbers, strings, arrays, etc. 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 a string output according to a specified format.
Some formatting standards:
%v
Output structure: {10 30}%+v
output structure field names {one:10 tow:30}%#v
output the source code fragment of the struct main.Point{one:10, two:30}%T
output the type of the value main.Point%t
Output formatted boolean true%d
Output standard decimal formatting 100%b
Output standard binary formatting 99 which is 1100011%c
Output the corresponding character for the fixed integer 99 corresponds to c%x
Output the hexadecimal encoding 99 corresponds to 63%f
Output the decimal formatted 99 corresponds to 63%e
Output the scientific notation representation of 123400000.0 as 1.234000e+08%E
Output the scientific notation representation of 123400000.0 as 1.234000e+08%s
Perform basic string output “\“string\”” corresponds to “string”%q
The output with double quotes as in the source code “\“string\”” corresponds to “\“string\””%p
Output the value of a pointer &jgt corresponds to 0xc00004a090%
Use numbers to control the output width. The default result uses right alignment and fills the blank spaces with spaces.%2.2f
Specify the output width for floating-point numbers 1.2 corresponds to 1.20%*2.2f
Specify the output width alignment for floating-point numbers, using-
Flag 1.2 corresponds to *1.20
Usage Instructions
stringformat
How to use the filter:
{{ obj|stringformat:"格式定义" }}
For example, to transform3.141592653
To format a number to retain 2 decimal places, you can write it like this:
{{ 3.141592653|stringformat:"%.2f" }}
# 显示结果
3.14
Here is an example demonstration
{{ simple.float|stringformat:"%.2f" }}
{{ simple.uint|stringformat:"Test: %d" }}
{{ simple.chinese_hello_world|stringformat:"Chinese: %s" }}
# 显示结果
Hello!
3.14
Test: 8
Chinese: 你好世界