Freemaker digital format in depth understanding

Time:2021-5-6

1、 Using built-in function C
characteristic:
No matter what the form of expression is (such as “123456.123456”, “123456.123456”, “000123456.123456000”, “123456.123456”, “12345,6.123456”), as long as the decimal separator is’. ‘, it can be converted into a string form that can be recognized by the computer, and it is 123456.123456. When the computer processes, there is no so-called thousand separator (space or comma), And the maximum number of decimal places supported by the computer is 16. At present, it is enough for us.
Usage:
Assuming that strmun is a number in the form of a string, you can write ${strnum? C}, for example, ${“123456.123456”? c} The value is 123456.123456
Possible uses:
For some forms, users are allowed to input numbers according to their personal habits and convert them when submitting

2、 Use predefined number formats
There are four pre-defined number formats: computer (the same function as built-in function C, but different usage), currency (currency format), number (number format), percentage (percentage format)
characteristic:
The explicit meaning of these formats is specified by the localization (country) and controlled by the Java platform installation environment rather than FreeMarker. Therefore, it is not recommended to use these functions. Moreover, due to the influence of the default number format, the usage is inflexible.

3、 Use a number format syntax similar to Java
characteristic:
For example, “0. #”, the number of ‘0’ on the left represents the minimum number of digits in the integer part, and the number of ‘# “on the right represents the maximum number of digits in the decimal part; For example “, ################################; For example, “0.%” is expressed as a percentage with a maximum of two decimal places.
Usage:
Assuming that strnum is a number, you can write ${strnum? String (“, ###.0 #)} like this, for example, ${123456.123456? String (“, ###0.0 #)} value is 123456.12
be careful:
The number format is localization sensitive, but we usually set the default localization language for freemaker, which is not a big problem.
Possible uses:
When you suddenly need to use the number format in a certain place, you can call the string function. When you need to use the number format in many places, it’s too troublesome. You can consider setting the default number format

4、 Local settings
characteristic:
If the number format used in a page or several pages (other pages can be imported) is the same, consider configuring the format of numbers uniformly
Usage:
Just set it before using the number to be formatted. The format is as follows: < settingnumber_ Format =, ##########################################.
be careful:
If it is used on a page, the default format of the whole page will be the same, unless the default format is overridden by the string function. Similarly, if it is placed on a public page, other pages will be in the same format as long as they are included.

5、 Global settings
characteristic:
The default format of numbers is provided on all pages
usage
(for spring): set the default number format in freemaker’s configuration file as follows:

Copy codeThe code is 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”)
characteristic:
The first three are easy to understand literally, and we often use them. For the last one, let’s take an example of 1.5? String (“0”) and 2.5? The values of string (“0”) are all 2. The description is as follows:
In finance and statistics, rounding is based on the so-called half principle, which means that the nearest “neighbor” is rounded, unless the distance from two neighbors is equal, in this case, it is rounded to an even number of neighbors. If you look at the rounding of 1.5 and 2.5, as you can see in the example above, both are rounded to 2, because 2 is even, but 1 and 3 are odd.
Usage: assuming that 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 (it may be useful in pagination processing), some cases that are sensitive to decimals, or string function
For example:

Copy codeThe code is as follows:
${num?string(‘0.00’)}

If there are less than two decimal places, use 0 instead

Copy codeThe code is as follows:
${num?string(‘#.##’)}

If there are more than two digits after the decimal point, only two digits will be reserved, otherwise the actual value will be output
The output is 1239765.46

Copy codeThe code is as follows:
${num?string(‘,###.00’)}

The output is 1239765.46
The integer part is divided into three parts, and two decimal places are reserved after the decimal point

Copy codeThe code is as follows:
${num?string(‘,###.##’)}

The output is 1239765.46
The integer part is divided into three parts. If there are more than two places after the decimal point, only two places will be reserved. If there are less than two places, the actual number will be taken. The decimal point may not be included

Copy codeThe code is as follows:
${num?string(‘000.00’)}

The output is: 012.70
If the integer part is less than three digits (000), use 0 in front of it, otherwise, take the actual integer digit

Copy codeThe code is as follows:
${num?string(‘###.00’)}

Equivalent to

Copy codeThe code is as follows:
${num?string(‘#.00’)}

The output is: 12.70
Operation problems caused by freemaker number format
When freemaker parses the data format, it automatically divides the number into three parts (1000) by default. This problem brings some extra processing complexity to the operation. The solutions are as follows:
1. Add. ToString () directly in the template to convert the number into a string, such as:

Copy codeThe code is as follows:
${languageList.id.toString()};

2. In the freemaker configuration file freemaker.properties, add

Copy codeThe code is as follows:
<#setting number_ Format = # “> or < setting number_ format=”0”>;

3. Add the setting number in the template directly_ Format = # “> or < setting number_ Format = “0” >, such as: < If

Copy codeThe code is as follows:
AdminLanguagePaginationMsg?exists>
<#setting number_format=”#”>

For the number format, you can use strng and number_ Format settings
Priority of number formatting: string has the highest priority, configuration file has the lowest priority, and the priority of page setting is between the two.

7、 Summary
For the number format, you can use strng and number_ Format settings
Priority of number formatting: string has the highest priority, configuration file has the lowest priority, and the priority of page setting is between the two.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]