SoFunction
Updated on 2025-04-06

Freemarker In-depth understanding of digital formatting

1. Use built-in function c
Features:
No matter what its manifestation is (for example, "123,456.123,456", "123456.123456", "000123456.123456000", "123,456.123456", "1,2345,6.123456"), as long as the decimal splitter is '.', it can be converted into a string form that the computer can recognize, and they are all 123456.123456. When the computer processes, there are no so-called thousand separators (spaces or commas), and the maximum number of digits after the decimal point supported by the computer is 16 digits. At present, it is enough for us to use it.
usage:
Assuming strmun is a number in the form of a string, you can write ${strnum?c} like this, for example, ${"123,456.123,456"?c} has a value of 123456.123456
Possible uses:
For some forms, users can enter numbers according to their personal habits and convert them uniformly when submitting.

2. Use predefined digital formats
There are four predefined number formats, namely computer (same as the built-in function c function, different usage), currency (currency format), number (number format), and percentage (percent form)
Features:
The explicit meaning of these formats is specified by localization (country) and is controlled by the Java platform installation environment, not FreeMarker. Therefore, these functions are not recommended, and they are affected by the default numeric format and are inflexible.

3. Use syntax similar to the number format in Java
Features:
For example, "0.#", the number of '0' on the left represents the least number of integer part, and the number of '#" on the right represents the most number of decimal part; for example, ",##0.0#" means that the thousand-digit splitter of the integer part is ',', the decimal part retains at most two digits, and the least one is retained; for example, "0.##%" is expressed in the form of a percentage, and the decimal part is at most two digits.
usage:
Assuming strnum is a number, you can write ${strnum?string(",##0.0#")} like this, for example, ${123456.123456?string(",##0.0#")} value is 123,456.12
Notice:
The digital format is localized and sensitive, but we usually set the default localization language for freemarker, which is not a big problem.
Possible uses:
When a number format is suddenly used in a certain place, it is OK to call the string function. When a number format is needed in many places, it is too troublesome to handle it. You can consider setting the default number format

4. Local settings
Features:
If the number format is the same on a certain page or several pages (other pages can be imported), the number formatting used is the same. You can consider configuring the formatting form of the number in a unified manner.
usage:
Just set the number before using the formatted number. The format is as follows: <#settingnumber_format=",##0.##">The content of ",##0.##" is a syntax form similar to the number format in Java, which is the third point above.
Notice:
If used on a page, the entire page will be in that format by default, unless the default format is overwritten with the string function. Similarly, if placed on a public page, other pages will have the same format as long as they include it.

5. Global settings
Features:
All pages are provided with the format of numbers by default
usage
(For spring): Set its default digital format in the freemarker configuration file, as follows:

Copy the codeThe code is as follows:

<propertyname="freemarkerSettings">
<props>
.....
<propkey="number_format">0.##</prop>
......
</props>
</property>


6. Rounding processing
There are several rounding processing methods, namely round, floor, ceiling and string("0")
Features:
The first three are easy to understand literally, and we often use them. To talk about the last one, let’s first look at an example of the values ​​of 1.5?string("0") and 2.5?string("0") are both 2. The explanation is as follows:
In finance and statistics, rounding is based on the so-called half principle, which means rounding the nearest "neighbor" unless it is equally distanced from the two neighbors, in which case it is rounded to an even number of neighbors. If you pay attention to rounding of 1.5 and 2.5, this is visible in the example above, both are rounded to 2 because 2 is an even number, but 1 and 3 are odd numbers.
Usage: Assuming strnum is a number, you can write ${strnum?round(floor/ceiling)} or ${strmun?string("0")} like this
Possible usage:
It can be used in some cases where the accuracy of numbers is not high (may be useful when paging), and some are sensitive to decimals, or use the string function.
For example:
Copy the codeThe code is as follows:

${num?string('0.00')}

If the decimal point is less than two digits, use 0 instead
Copy the codeThe code is as follows:

${num?string('#.##')}

If two extra digits are left after the decimal point, only two digits will be retained, otherwise the actual value will be output.
Output is: 1239765.46
Copy the codeThe code is as follows:

${num?string(',###.00')}

Output is: 1,239,765.46
The integer part is divided by , and it is guaranteed to retain two digits after the decimal point. If it is insufficient, use 0 instead.
Copy the codeThe code is as follows:

${num?string(',###.##')}

Output is: 1,239,765.46
The integer part is divided by , every three digits, and the extra two digits are retained after the decimal point. If there are less than two digits, the actual digits are taken, and the decimal point can be included.
Copy the codeThe code is as follows:

${num?string('000.00')}

Output is: 012.70
If the integer part is less than three digits (000), use 0 to fill it in the first place, otherwise take the actual integer bits.
Copy the codeThe code is as follows:

${num?string('###.00')}

Equivalent to
Copy the codeThe code is as follows:

${num?string('#.00')}

Output is: 12.70
Operational problems caused by freemarker digital formatting
When freemarker parses the data format, it automatically divides the number into 3 by default (1,000). This problem brings a certain amount of additional processing complexity to the operation. The solutions are as follows:
1. Add .toString() directly to the template to convert the number into a string, such as:
Copy the codeThe code is as follows:

${()};

2. Add in the freemarker configuration file
Copy the codeThe code is as follows:

<#setting number_format="#">or <#setting number_format="0">;

3. Add <#setting number_format="#"> or <#setting number_format="0"> directly to the template, such as: <#if
Copy the codeThe code is as follows:

AdminLanguagePaginationMsg?exists>
<#setting number_format="#">

For formatting of numbers, you can use strng and number_format settings
Priority for digital formatting: string has the highest priority, configuration file configuration has the lowest priority, and the priority for page setting is between the two.

7. Summary
For formatting of numbers, you can use strng and number_format settings
Priority for digital formatting: string has the highest priority, configuration file configuration has the lowest priority, and the priority for page setting is between the two.