How to format any value such as numbers, strings, arrays, etc. into a string with a specified format in the Anqi CMS template?
stringformatThe filter can format any value such as numbers, strings, arrays, etc. into a string output according to a specified format.
Some formatting standards:
%vOutput structure: {10 30}%+voutput structure field names {one:10 tow:30}%#voutput the source code fragment of the struct main.Point{one:10, two:30}%Toutput the type of the value main.Point%tOutput formatted boolean true%dOutput standard decimal formatting 100%bOutput standard binary formatting 99 which is 1100011%cOutput the corresponding character for the fixed integer 99 corresponds to c%xOutput the hexadecimal encoding 99 corresponds to 63%fOutput the decimal formatted 99 corresponds to 63%eOutput the scientific notation representation of 123400000.0 as 1.234000e+08%EOutput the scientific notation representation of 123400000.0 as 1.234000e+08%sPerform basic string output “\“string\”” corresponds to “string”%qThe output with double quotes as in the source code “\“string\”” corresponds to “\“string\””%pOutput 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.2fSpecify the output width for floating-point numbers 1.2 corresponds to 1.20%*2.2fSpecify the output width alignment for floating-point numbers, using-Flag 1.2 corresponds to *1.20
Usage Instructions
stringformatHow to use the filter:
{{ obj|stringformat:"格式定义" }}
For example, to transform3.141592653To 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: 你好世界