How to format numbers, strings, arrays, and any other values into a string in AnQi CMS template?
stringformatThe filter can format numbers, strings, arrays, and any other values into strings in a specified format.
Some formatting standards:
%vOutput the structure {10 30}%+vOutput structure display field name {one:10 tow:30}%#vOutput the source code fragment of the structure main.Point{one:10, tow:30}%TOutput the type of the value main.Point%tOutput formatted boolean true%dOutput standard decimal formatting 100%bOutput standard binary formatting 99 corresponds to 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 form 123400000.0 corresponds to 1.234000e+08%EOutput the scientific notation representation form 123400000.0 corresponds to 1.234000e+08%sPerform basic string output “\“string\”” corresponds to “string”%qThe output of the source code with double quotes “\“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 space with spaces%2.2fSpecify the output width of the floating-point type 1.2 corresponds to 1.20%*2.2fSpecify the width alignment for floating-point output using-The symbol 1.2 corresponds to *1.20
Usage method
stringformatHow to use the filter:
{{ obj|stringformat:"格式定义" }}
For example, to convert3.141592653To format a number to retain two decimal places, you can write it as:
{{ 3.141592653|stringformat:"%.2f" }}
# 显示结果
3.14
Example Demonstration
{{ simple.float|stringformat:"%.2f" }}
{{ simple.uint|stringformat:"Test: %d" }}
{{ simple.chinese_hello_world|stringformat:"Chinese: %s" }}
# 显示结果
Hello!
3.14
Test: 8
Chinese: 你好世界