Freemarker number format in JSP

Source: Internet
Author: User
Tags split

First, use the built-in function C

Characteristics:

Regardless of the form of expression (

For example "123,456.123,456", "123 456.123 456", "000 123 456.123 456 000", "123,456.123 456", "1,2345,6.123456")

, as long as the decimal separator is '. ' it can be converted into a string form that the computer can recognize, and it is all 123456.123456, and the computer is processing without those so-called thousand separators (spaces or commas), and the computer supports the largest number of digits after the decimal point is 16 digits, It's enough for us to use.

Usage:

Suppose Strmun is a string-like number, you can write ${strnum?c} such as ${"123,456.123,456". C} Value 123456.123456

Possible uses:

For some forms allow users to enter numbers according to their personal habits, when submitting a unified conversion

Ii. use of predefined number formats

There are four predefined number formats, respectively, computer (like the built-in function C function, usage is not the same), currency (currency format), number (numeric format), percent (percent form)

Characteristics:

The explicit meaning of these formats is localized (country)-specific, and is controlled by the Java platform installation environment, rather than freemarker, so these functions are not recommended and are affected by the default number format and are not flexible to use.

Third, the use of similar to Java in the form of digital format syntax

Characteristics:

For example "0.#", the number of ' 0 ' on the left represents the minimum number of digits in the integer part, the number of "#" on the right represents the maximum number of digits in the decimal portion; For example, ", # #0.0#" means that the thousand separator for the integer part is ', ' and the fractional part retains up to two bits, at least one bit; for example, "0.##%" As a percentage, the decimal portion is up to two digits.

Usage:

Assuming that Strnum is a number, you can write ${strnum?string (", # #0.0#")}, such as ${123456.123456?string (", # #0.0#")} value is 123,456.12

Attention:

The number format is localized sensitive, but we generally set the default localized language for Freemarker, which is not a problem.

Possible uses:

When you suddenly want to use the number format in a certain place, it is OK to call the string function, when you need to use the number format in many places, this is too cumbersome to handle, you can consider setting the default number format form

Four, Local settings

Characteristics:

If a page, or a few pages (other pages can be import in), the number format used is the same, you can consider the format of unified configuration numbers

Usage:

Just set it up before you use the number you need to format, as follows: < #setting number_format= ", # #0. # #" > where ", # #0. # #" The content is a syntax similar to the number format in Java, which is the 3rd above. "

Attention:

If you use a page, the entire page defaults to that format, unless you override the default format with a string function, and if you put it on a public page, the other page will be the same format as long as it is available.

Five, global settings

Characteristics:

The format of numbers is provided by default on all pages

Usage

(for spring): sets its default number format in the Freemarker configuration file as follows:

The code is as follows

< property name = "Freemarkersettings" >

< props>

.....

< prop key = "Number_format" > 0.##

......

  

  

Vi. Rounding Processing

There are several rounding methods, round,floor,ceiling and string ("0"), respectively.

Characteristics:

The first three kinds, literally well understood, we also often use, to say the last one, look at an example the values of 1.5?string ("0") and 2.5?string ("0") are all 2 descriptions as follows:

In finance and statistics, rounding is based on the so-called half principle, which means rounding up the nearest "neighbor" unless it is equal to two neighbors, in which case it is rounded to an even neighbor. If you look at the rounding of 1.5 and 2.5, as you can see in the example above, two are rounded to 2 because 2 is an even number, but 1 and 3 are odd.

Usage: Suppose Strnum is a number, you can write ${strnum?round (floor/ceiling)} or ${strmun?string ("0")}

Possible usage:

Can be used in cases where the accuracy of the numbers is not high (may be useful when paging processing), some are sensitive to decimal, or the string function

For example:

${num?string (' 0.00 ')}

If less than two digits after the decimal point, replace with 0

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

If you have more than two digits after the decimal point, only two digits are left, otherwise the actual value is output

Output as: 1239765.46

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

Output is: 1,239,765.46

Integer part of every three bits, split, and ensure that after the decimal point to retain two digits, not enough to replace the 0

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

Output is: 1,239,765.46

Integer part of every three bits, split, and after the decimal point of more than two digits to retain only two digits, less than two digits to take the actual digits, can not contain a decimal point

${num?string (' 000.00 ')}

Output as: 012.70

If the integer part is less than three digits (000), the front is padded with 0, otherwise the actual integer digit is taken

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

Equivalent to

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

Output as: 12.70

Freemarker the operational problems caused by digital formatting

Freemarker in parsing the data format, the automatic default of the number by 3 for the division (1,000), the problem to the operation of a certain amount of additional processing complexity, the solution is as follows:

1, in the template directly plus. toString () Converts the number to a string, such as:

${languagelist.id.tostring ()};

2, in the Freemarker configuration file freemarker.properties Plus

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

3, in the template directly add < #setting number_format= "#" > or < #setting number_format= "0", such as:< #if

Adminlanguagepaginationmsg?exists>

< #setting number_format= "#" >

For the formatting of numbers, you can use the STRNG and Number_format settings

Number formatting priority: String priority is highest, configuration file configuration has the lowest priority, the page setting priority in between.

The actual number of digits for more usage of the string directive, you can access the official documentation: HTTP://FREEMARKER.SOURCEFORGE.NET/DOCS/REF_BUILTINS_NUMBER.HTML#REF_ Builtin_string_for_number

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.