I. Use the built-in function c
Features:
Regardless of the form (
The code is as follows: |
Copy code |
For example, "123,456.123, 456", "123 456.123 456 123", "000 456.123 456 123,456.123", "456 6.123456 ") |
, As long as the decimal separator is '. 'It can be converted into a string that can be recognized by the computer, and all of them are 123456.123456. When the computer is processing, there are no so-called thousands of delimiters (spaces or commas ), in addition, the computer supports a maximum of 16 digits after the decimal point. Currently, it is enough for us to use.
Usage:
Assume that strmun is a string number, you can write $ {strnum? C}, for example, $ {"123,456.123, 456 "? C} is 123456.123456
Possible use:
Some forms allow users to enter numbers according to their personal habits, which can be converted during submission.
II. Use the predefined numeric format
There are four predefined numeric formats: computer (similar to the built-in function c), currency (currency format), and number (numeric format), percent (percent form)
Features:
The explicit meaning of these formats is specified by the localization (country) and is controlled by the Java platform installation environment, rather than FreeMarker. Therefore, these functions are not recommended and are affected by the default numeric format, the usage is not flexible.
3. Use a number format similar to the number format syntax in Java
Features:
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 ", #0.0 # "indicates that the delimiter of the integer part is ',', and the fractional part can retain at most two digits, with at least one digit. For example," 0. # % "is expressed as a percentage. The decimal part can be at most two digits.
Usage:
If strnum is a number, you can write $ {strnum? String (", #0.0 #")}, for example, $ {123456.123456? String (", #0.0 #")} is 123,456.12
Note:
The digital format is localized and sensitive, but we usually set the default localization language for freemarker, which is not a problem.
Possible use:
When the numeric format is suddenly used in a certain place, it is okay to call the string function. When the numeric format is used in many places, it is too troublesome to handle it, you can set the default number format.
IV. Local settings
Features:
If the numbers used for formatting are the same on a page or several pages (other pages can be imported), you can configure the format of numbers in a unified manner.
Usage:
You only need to set the number before formatting. The format is as follows: <# setting number_format = ", #0. # "> where", #0. # "The content is similar to the number format in java, that is, the third point above.
Note:
If a page is used, the entire page is in that format by default, unless the default format is overwritten by the string function. Similarly, if it is placed on a public page, other pages only need to include it, it will also be in the same format.
5. Global Settings
Features:
By default, numbers are formatted on all pages.
Usage
(For spring): in the configuration file of freemarker, set its default numeric format, as follows:
The code is as follows: |
Copy code |
<Property name = "freemarkerSettings"> <Props> ..... <Prop key = "number_format"> 0. ##</prop> ...... </Props> </Property> |
6. Rounding
There are several rounding methods: round, floor, ceiling, and string ("0 ")
Features:
The first three types are literally easy to understand, and we often use them. Let's first look at an example 1.5? String ("0") and 2.5? The values of string ("0") are both 2:
In finance and statistics, rounding is based on the so-called half principle, which means rounding the nearest "neighbor" unless the distance between the two neighbors is equal. In this case, it is rounded to an even number of neighbors. If you pay attention to rounding 1.5 and 2.5, we can see in the above example that both are rounded to 2, because 2 is an even number, but 1 and 3 are odd.
Usage: if strnum is a number, you can write $ {strnum? Round (floor/ceiling)} or $ {strmun? String ("0 ")}
Possible usage:
It can be used in cases with low numeric precision (paging may be useful) and decimal-sensitive cases, or using the string function.
For example:
The code is as follows: |
Copy code |
$ {Num? String ('0. 00 ')}
|
If there are less than two digits after the decimal point, use 0 instead.
The code is as follows: |
Copy code |
$ {Num? String ('#.##')}
|
If there are two more digits after the decimal point, only two digits are retained; otherwise, the actual value is output.
Output: 1239765.46
The code is as follows: |
Copy code |
$ {Num? String (', ###. 00 ')} Output: 1,239,765.46
|
Each three digits of the integer are separated by commas (,), and two digits are retained after the decimal point. If not, use 0 instead.
The code is as follows: |
Copy code |
$ {Num? String (',###.##')} Output: 1,239,765.46
|
An integer is separated by three digits. If there are two digits after the decimal point, only two digits are retained. If there are less than two digits, the actual number of digits is obtained. The decimal point can be excluded.
The code is as follows: |
Copy code |
$ {Num? String ('000. 00 ')} Output: 012.70
|
If the integer is less than three digits (000), the first digit is filled with 0. Otherwise, the actual integer is obtained.
The code is as follows: |
Copy code |
$ {Num? String ('###. 00 ')} Equivalent $ {Num? String ('#. 00 ')} Output: 12.70 |
Operation problems caused by freemarker numeric formatting
Freemarker automatically splits the number by 3 (1,000) by default when parsing the data format. This problem brings some extra processing complexity to the operation. There are several solutions:
1. Directly add. toString () in the template to convert the number to a string, for example:
The code is as follows: |
Copy code |
$ {LanguageList. id. toString ()}; |
2. Add freemarker. properties in the freemarker configuration file
The code is as follows: |
Copy code |
<# Setting number_format = "#"> or <# setting number_format = "0">; |
3. Directly add <# setting number_format = "#"> or <# setting number_format = "0"> to the template, for example: <# if
The code is as follows: |
Copy code |
Admin1_agepaginationmsg? Exists> <# Setting number_format = "#"> |
You can use strng and number_format to format numbers.
Priority of number formatting: string has the highest priority and configuration file has the lowest priority. The priority of setting on the page is between the two.
Integer to take the actual number of digits about the string command more use method, you can visit the official documentation: http://freemarker.sourceforge.net/docs/ref_builtins_number.html#ref_builtin_string_for_number