MyException - 我的异常网
当前位置:我的异常网» 软件架构设计 » 应用freemarker的一点心得

应用freemarker的一点心得

www.MyException.Cn  网友分享于:2013-11-07  浏览:14次
使用freemarker的一点心得!
在开发中很容易忽视一点,输入一个值(可能是小数),输出时如果不做处理,就很容易出现

隐形的BUG。比如,如果从数据库取出一个0.22的数值,一般的输出${x?if_exists?html},

这时是显示0,而不是0.22。

应该写成${x?if_exists?string.number} 或者 ${x?if_exists.toString()?html}

下面就是关于数字的具体介绍:

Built-ins for numbers

Related FAQs: Do you have things like 1,000,000 or 1 000 000 instead of 1000000, or something like 3.14 instead of 3,14 or vice versa? See this and this FAQ entry, also note the c built-in above.
c
Note

This built-in exists since FreeMarker 2.3.3.

This built-in converts a number to string for ``computer audience'' as opposed to human audience. That is, it formats with the rules that programming languages used to use, which is independent of all the locale and number format settings of FreeMarker. It always uses dot as decimal separator, and it never uses grouping separators (like 3,000,000), nor exponential form (like 5E20), nor superfluous leading or trailing 0-s (like 03 or 1.0), nor + sign (like +1). It will print at most 16 digits after the decimal dot, and thus numbers whose absolute value is less than 1E-16 will be shown as 0. This built-in is crucial because be default (like with ${x}) numbers are converted to strings with the locale (language, country) specific number formatting, which is for human readers (like 300000 is possibly printed as 3,000,000). When the number is printed not for human audience (e.g., for a database record ID used as the part of an URL, or as invisible field value in a HTML form, or for printing CSS/JavaScript numerical literals) this built-in must be used to print the number (i.e., use ${x?c} instead of ${x}), or else the output will be possibly broken depending on the current number formatting settings and locale (like the decimal point is not dot, but comma in many countries) and the value of the number (like big numbers are possibly ``damaged'' by grouping separators).
string (when used with a numerical value)

Converts a number to a string. It uses the default format that the programmer has specified. You can also specify a number format explicitly with this built-in, as it will be shown later.

There are three predefined number formats: number, currency, and percent. The exact meaning of these is locale (nationality) specific, and is controlled by the Java platform installation, rather than by FreeMarker. You can use these predefined formats like this:



<#assign x=42>
${x}
${x?string}  <#-- the same as ${x} -->
${x?string.number}
${x?string.currency}
${x?string.percent} 




If your locale is US English, this will certainly produce:



42
42
42
$42.00
4,200% 




The output of first three expressions is identical because the first two expressions use the default format, which is "number" here. You can change this default using a setting:



<#setting number_format="currency">
<#assign x=42>
${x}
${x?string}  <#-- the same as ${x} -->
${x?string.number}
${x?string.currency}
${x?string.percent} 




Will now output:



$42.00
$42.00
42
$42.00
4,200% 




since the default number format was set to "currency".

Beside the three predefined formats, you can use arbitrary number format patterns written in Java decimal number format syntax:



<#assign x = 1.234>
${x?string("0")}
${x?string("0.#")}
${x?string("0.##")}
${x?string("0.###")}
${x?string("0.####")}

${1?string("000.00")}
${12.1?string("000.00")}
${123.456?string("000.00")}

${1.2?string("0")}
${1.8?string("0")}
${1.5?string("0")} <-- 1.5, rounded towards even neighbor
${2.5?string("0")} <-- 2.5, rounded towards even neighbor

${12345?string("0.##E0")} 




outputs this:



1
1.2
1.23
1.234
1.234

001.00
012.10
123.46

1
2
2 <-- 1.5, rounded towards even neighbor
2 <-- 2.5, rounded towards even neighbor

1.23E4 




Following the financial and statistics practice, the rounding goes according the so called half-even rule, which means rounding towards the nearest ``neighbor'', unless both neighbors are equidistant, in which case, it rounds towards the even neighbor. This was visible in the above example if you look at the rounding of 1.5 and of 2.5, as both were rounded to 2, since 2 is even, but 1 and 3 are odds.

Appart from the Java decimal syntax patterns, you can also write ${aNumber?string("currency")} and like, that will do the same as ${aNumber?string.currency} and like.

As it was shown for the predefined formats earlier, the default formatting of the numbers can be set in the template:



<#setting number_format="0.##">
${1.234} 




outputs this:



1.23 




Note that the number formatting is locale sensitive:



<#setting locale="en_US">
US people write:        ${12345678?string(",##0.00")}
<#setting locale="hu">
Hungarian people write: ${12345678?string(",##0.00")} 




outputs this:



US people write:        12,345,678.00
Hungarian people write: 12 345 678,00 




You can find information about the formatting of dates here.
round, floor, ceiling
Note

The rounding built-ins exist since FreeMarker 2.3.13.

Converts a number to a whole number using the specified rounding rule:

    *

      round: Rounds to the nearest whole number. If the number ends with .5, then it rounds upwards (i.e., towards positive infinity)
    *

      floor: Rounds the number downwards (i.e., towards neagative infinity)
    *

      ceiling: Rounds the number upwards (i.e., towards positive infinity)

Example:



<#assign testlist=[
  0, 1, -1, 0.5, 1.5, -0.5,
  -1.5, 0.25, -0.25, 1.75, -1.75]>
<#list testlist as result>
    ${result} ?floor=${result?floor} ?ceiling=${result?ceiling} ?round=${result?round}
</#list>
 




Prints:



    0 ?floor=0 ?ceiling=0 ?round=0           
    1 ?floor=1 ?ceiling=1 ?round=1       
    -1 ?floor=-1 ?ceiling=-1 ?round=-1     
    0.5 ?floor=0 ?ceiling=1 ?round=1     
    1.5 ?floor=1 ?ceiling=2 ?round=2     
    -0.5 ?floor=-1 ?ceiling=0 ?round=0    
    -1.5 ?floor=-2 ?ceiling=-1 ?round=-1   
    0.25 ?floor=0 ?ceiling=1 ?round=0    
    -0.25 ?floor=-1 ?ceiling=0 ?round=0   
    1.75 ?floor=1 ?ceiling=2 ?round=2    
    -1.75 ?floor=-2 ?ceiling=-1 ?round=-2    




These built-ins may be useful in pagination operations and like. If you just want to display numbers in rounded form, then you should rather use the string built-in or the number_format setting.


也可以直接看原文:
http://freemarker.org/docs/ref_builtins_number.html#ref_builtin_string_for_number
1 楼 flowerdance 2008-06-26  
...........好用吗?我怎么觉得怪怪的
2 楼 limeng530_1987 2008-06-26  
现在也在学习FREEMARKER
3 楼 vaja 2008-11-05  
在FTL页一开始写上 <#setting number_format="0.#">
就可以保留一位小数
4 楼 gongstring 2009-06-09  
很好,今天正好用上了!谢谢了

文章评论

亲爱的项目经理,我恨你
亲爱的项目经理,我恨你
60个开发者不容错过的免费资源库
60个开发者不容错过的免费资源库
如何成为一名黑客
如何成为一名黑客
“肮脏的”IT工作排行榜
“肮脏的”IT工作排行榜
Web开发者需具备的8个好习惯
Web开发者需具备的8个好习惯
我的丈夫是个程序员
我的丈夫是个程序员
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
如何区分一个程序员是“老手“还是“新手“?
如何区分一个程序员是“老手“还是“新手“?
不懂技术不要对懂技术的人说这很容易实现
不懂技术不要对懂技术的人说这很容易实现
程序员的一天:一寸光阴一寸金
程序员的一天:一寸光阴一寸金
那些争议最大的编程观点
那些争议最大的编程观点
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
老美怎么看待阿里赴美上市
老美怎么看待阿里赴美上市
初级 vs 高级开发者 哪个性价比更高?
初级 vs 高级开发者 哪个性价比更高?
 程序员的样子
程序员的样子
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
为啥Android手机总会越用越慢?
为啥Android手机总会越用越慢?
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
总结2014中国互联网十大段子
总结2014中国互联网十大段子
中美印日四国程序员比较
中美印日四国程序员比较
鲜为人知的编程真相
鲜为人知的编程真相
团队中“技术大拿”并非越多越好
团队中“技术大拿”并非越多越好
程序员和编码员之间的区别
程序员和编码员之间的区别
写给自己也写给你 自己到底该何去何从
写给自己也写给你 自己到底该何去何从
当下全球最炙手可热的八位少年创业者
当下全球最炙手可热的八位少年创业者
看13位CEO、创始人和高管如何提高工作效率
看13位CEO、创始人和高管如何提高工作效率
Web开发人员为什么越来越懒了?
Web开发人员为什么越来越懒了?
十大编程算法助程序员走上高手之路
十大编程算法助程序员走上高手之路
每天工作4小时的程序员
每天工作4小时的程序员
程序员最害怕的5件事 你中招了吗?
程序员最害怕的5件事 你中招了吗?
我是如何打败拖延症的
我是如何打败拖延症的
我跳槽是因为他们的显示器更大
我跳槽是因为他们的显示器更大
代码女神横空出世
代码女神横空出世
科技史上最臭名昭著的13大罪犯
科技史上最臭名昭著的13大罪犯
一个程序员的时间管理
一个程序员的时间管理
编程语言是女人
编程语言是女人
程序员都该阅读的书
程序员都该阅读的书
“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
Java 与 .NET 的平台发展之争
Java 与 .NET 的平台发展之争
老程序员的下场
老程序员的下场
10个调试和排错的小建议
10个调试和排错的小建议
程序员必看的十大电影
程序员必看的十大电影
聊聊HTTPS和SSL/TLS协议
聊聊HTTPS和SSL/TLS协议
为什么程序员都是夜猫子
为什么程序员都是夜猫子
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有