Freemarker digital formatting

Source: Internet
Author: User

I. Use the built-in Function c
Features:
Regardless of the form (for example, "123,456.123, 456", "123456.123456", "000123456.123456000", "123,456.123456", "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: <# settingnumber_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:
Copy codeThe Code is as follows:
<Propertyname = "freemarkerSettings">
<Props>
.....
<Propkey = "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:
Copy codeThe Code is as follows:
$ {Num? String ('0. 00 ')}

If there are less than two digits after the decimal point, use 0 instead.
Copy codeThe Code is as follows:
$ {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
Copy codeThe Code is as follows:
$ {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.
Copy codeThe Code is as follows:
$ {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.
Copy codeThe Code is as follows:
$ {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.
Copy codeThe Code is as follows:
$ {Num? String ('###. 00 ')}

Equivalent
Copy codeThe Code is as follows:
$ {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:
Copy codeThe Code is as follows:
$ {LanguageList. id. toString ()};

2. Add freemarker. properties in the freemarker configuration file
Copy codeThe Code is as follows:
<# 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
Copy codeThe Code is as follows:
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.

VII. Summary
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.