Freemarker all kinds of formatting

Source: Internet
Author: User

1. Formatted date

${updated?string ("Yyyy-mm-dd HH:mm:ss")}  

If the specified variable does not necessarily exist, you can:

${(Datemap.begintime?string ("yyyy. MM.DD "))!  

2. Display a Boolean value

< #assign foo=true/>  ${foo?string ("Yes", "No")}  

3. Intercept string length

<#if (uservo.cnname)?? && ((uservo.cnname) length > Ten) >  ${uservo.cnnamesubstring (0,10)}..    <#Else>  ${(uservo.cnname)! " }  </#if >  

4. Number format
Three number formats were booked in Freemarker: number,currency (currency) and percent (percent) where number is the default numeric format conversion
For example:

< #assign tempnum=20>${tempnum}    ${tempnum? String.number} or ${tempnum?  String ("number")}  result is 20${tempnum? String.currency} or ${tempnum?string ("Currency")}  result is ¥20.00  ${tempnum? string. Percent} or ${tempnum?string ("percent")}  result is 2,000%

Freemarker number formatting can be set in two places, one is global, that is, setting Number_format in the Freemarker.properties file, and the other is to use the string instruction to control the output format of the data. For example:

${num?string (' 0.00 ')}

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

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

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

output is: 1,239,765.46

Integer part every three bits used, split, and guaranteed to retain two digits after the decimal point, less than 0 instead of
${num?string (', ###.## ')}

output is: 1,239,765.46

Integer part of every three bits with, split, and after the decimal point extra two bits will only retain two bits, less than two digits to take the actual number of digits, you can not include the decimal point
${num?string (' 000.00 ')}

output is: 012.70

If the integer part is less than three bits (000), the front is padded with 0, otherwise the actual integer digits are taken.
${num?string (' ###.00 ')} is equivalent to ${num? string (' #.00 ')}

output is: 12.70

Integer takes the actual number of digits

Freemarker all kinds of formatting

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.