In depth understanding of freemarker number formatting

  • 2020-05-17 06:09:11
  • OfStack

1. Use the built-in c function
Features:
No matter what the representation is (e.g. "123,456.123,456", "123456.123456", "000123456.12456000", "123,456.123456", "1,2345,6.123456"), as long as the decimal separator is '.' it can be converted to a string recognized by the computer, and it is always 123456.123456. And the maximum number of digits behind the decimal point supported by the computer is 16, which is enough for us at the moment.
Usage:
If strmun is a number in the form of a string, write ${strnum? c}, for example, ${" 123456123, 456 "? c} a value of 123456.123456
Possible USES:
Some forms allow users to enter Numbers based on their personal preferences and convert them when they submit

2. Use a predefined number format
There are four predefined number formats: computer (not the same as the built-in function c), currency (currency format), number (number format), percent (percentage format)
Features:
The explicit meaning of these formats is localized (country-specific) and is controlled by the Java platform installation environment, not FreeMarker, so these functions are not recommended and are not flexible due to the default numeric format.

3. Use a number formatting syntax similar to Java
Features:
For example, "0.#", the number of '0' on the left represents the number of bits at least in the integer part, and the number of '# 'on the right represents the number of bits at most in the decimal part; For example, ",##0.0#" means that the thousands separator of the integer part is ", ", and the decimal part is reserved at most two places and at least one place. For example, "0.##%" is expressed as a percentage, with two decimal places at most.
Usage:
If strnum is a number, write ${strnum? string (", 0.0 # # # ")}, for example, ${123456.123456? string (", 0.0 # # # ")} value is 123456.12
Note:
The digital format is location-sensitive, but we generally set the default localization language for freemarker, which is fine.
Possible USES:
It is ok to call the string function when a number format is suddenly needed in one place. It is too troublesome to use the number format in many places, so you can consider setting the default number format

4. Local Settings
Features:
If you are using the same number formatting on a page, or on several pages (other pages can be entered by import), consider configuring the number formatting in one format
Usage:
Just set it up before you use the number you want to format. The format is as follows: < #settingnumber_format=",##0.##" > The content of ",##0.##" is syntactically similar to the number format in java, which is point 3 above.
Note:
If used on one page, the entire page will be in that format by default, unless the default format is overridden by the string function. Similarly, if placed on a public page, other pages will be in the same format as include.

5. Global Settings
Features:
Formatting of Numbers is provided by default on all pages
usage
(for spring) : set the default numeric format in freemarker's configuration file, as follows:

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


6. Rounding processing
There are several rounding methods: round,floor,ceiling and string("0")
Features:
The first three, which are literally easy to understand, and we use a lot, the last one, let's look at an example, 1.5? string (" 0 ") and 2.5? The value of string("0") is 2, which is explained as follows:
In finance and statistics, a four-by-five round is based on the so-called one-half rule, which means a four-by-five round to the nearest neighbor, unless it is an even distance away, in which case it rounds four to an even number of neighbors. If you look at the 4 rounded 5 for 1.5 and 2.5, which you can see in the above example, both are rounded 5 by 4 to 2, because 2 is even, but 1 and 3 are odd.
Usage: if strnum is a number, write ${strnum? round (floor/ceiling)} and ${strmun? string (" 0 ")}
Possible USES:
It can be used in cases where number accuracy is not high (paging may be useful), or in cases where decimal is sensitive, using the string function
Such as:
 
${num?string('0.00')} 

If you have less than two decimal places, replace them with zeros
 
${num?string('#.##')} 

If there are more than two Spaces after the decimal point, keep only two, otherwise output the actual value
The output is: 1239765.46
 
${num?string(',###.00')} 

The output is: 1,239,765.46
Divide the whole number into 3 digits, and make sure you keep two decimal places, not enough to replace with 0's
 
${num?string(',###.##')} 

The output is: 1,239,765.46
The whole number is divided into three digits, and if there are more than two decimal places, then only two, and if there are less than two decimal places, then the actual number is taken, and the decimal point is not included
 
${num?string('000.00')} 

The output is: 012.70
If the integer part is less than 3 bits (000), fill it with 0 before, otherwise take the actual integer bit
 
${num?string('###.00')} 

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

The output is: 12.70
Operational problems with freemarker number formatting
When parsing the data format, freemarker automatically splits the Numbers by 3 (1,000) by default. This problem brings a certain amount of extra processing complexity to the operation. The solutions are as follows:
1. Directly add.toString() to the template to convert Numbers into strings, such as:
 
${languageList.id.toString()} ;  

2. Add in freemarker configuration file freemarker.properties
 
<#setting number_format="#"> or  <#setting number_format="0"> ;  

3. Add directly to the template < #setting number_format="#" > or < #setting number_format="0" > , such as: < #if
 
${num?string('0.00')} 
0
For number formatting, you can use the strng and number_format Settings
Priority of number formatting: string has the highest priority, configuration file configuration has the lowest priority, and setting on the page is in between.

7. To summarize
For number formatting, you can use strng and number_format Settings
Priority of number formatting: string has the highest priority, configuration file configuration has the lowest priority, and setting on the page is in between.


Related articles: