- 浏览: 440162 次
- 性别:
- 来自: 无锡
文章分类
最新评论
-
belonghu:
这还和字段是否主键,加索引,有很大关系,我对一个索引的字段查询 ...
MYSQL每日一用:SELECT 语句中比对(between and \ like \ left) -
lqingqingzijin:
好,就是想知道怎样将默认bash修改成nologin
我怎么创建和修改用户帐号,让它有一个nologin shell? -
yangxiutian:
“在jar中添加了字体”是什么意思?
java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:299) -
ljhard_1030:
楼主学习了,以后继续发表这类的文章,会继续光临的。。
刨根问底(Proxool连接池设置) -
RobustTm:
Selenium中使用的貌似是Junit 3.x,上面的例子也 ...
使用Selenium 和Junit 进行WEB功能测试
在开发中很容易忽视一点,输入一个值(可能是小数),输出时如果不做处理,就很容易出现
隐形的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
隐形的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
评论
4 楼
gongstring
2009-06-09
很好,今天正好用上了!谢谢了
3 楼
vaja
2008-11-05
在FTL页一开始写上 <#setting number_format="0.#">
就可以保留一位小数
就可以保留一位小数
2 楼
limeng530_1987
2008-06-26
现在也在学习FREEMARKER
1 楼
flowerdance
2008-06-26
...........好用吗?我怎么觉得怪怪的
发表评论
-
activemq 使用经验
2011-07-19 00:14 7021ActiveMQ 是apache的一个开源JMS服务器, ... -
quartz无法启动的原因
2011-04-21 21:06 3461quartz无法启动的原因 场景:spring集 ... -
使用Selenium 和Junit 进行WEB功能测试
2010-10-12 16:54 17694下载 用firefox 到http://seleni ... -
java反编译工具
2010-07-21 10:56 1171这款反编译器叫 "Java Decompiler ... -
运行一个可执行的Jar时,Classpath的设置无效问题,java的-cp和-jar参数不能
2010-07-12 14:26 3811情况为:在cmd中,运行java -jar *.jar,出现c ... -
实践中整理出tomcat集群和负载均衡
2010-07-09 18:18 993实践中整理出tomcat集群和负载均衡 ... -
linux JAVA环境安装和配置
2010-07-06 11:19 0服务器安装配置: linux创建ftp用户 ... -
tomcat 日志文件catalina按日划分
2010-06-13 13:54 1653#!/bin/bash cd `dirname $0` ... -
java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:299)
2010-05-26 22:11 4885在做项目时,老是第一次报一下错误: java.u ... -
URL中中文乱码
2010-05-10 13:28 1149乱码有时候是让我们最头疼,但是根据多年的经验: ... -
解决Linux下Java中文乱码问题
2010-04-13 12:45 21604情况说明: 本地测试数据正常,发布到服务器(centos)后 ... -
正则表达式匹配中文字符,Ctrl+F的福音!
2010-03-23 00:06 1843匹配中文字符的正则表达式: [\u4e00-\u9fa5] ... -
代码严谨度的重要性和耦合的重要性
2010-03-08 17:03 1303情况是:星期六,同时打我电话,说平台动不了,让我解决一下 ... -
测试:strust1.0+jsp做出的系统中出现的串号问题
2010-02-03 10:52 0测试:strust1.0+jsp做出的系统中出现的串号问题 ... -
java 中 List<String> 拆分成 带标记的 List<String>
2009-12-23 15:38 2979public class TransList { ... -
error:SQLServer 2000 Driver for JDBC]Broken pipe
2009-12-21 11:39 2116环境是:linux(red hat) tomcat5.0 j ... -
java或web中解决所有路径问题(最全分析绝对有你要的)
2009-08-25 14:01 2031java或web中解决所有路径 ... -
ibatis 保存修改时都是乱码
2009-07-22 13:21 1918ibatis 保存修改时都是乱码? 这个问题困恼了我好 ... -
AXIS2 学习心得
2009-07-03 17:47 1751首先,想大家介绍一个非常不错的学习axis2的教学网址: ... -
乱码问题——常量提示录——提示Java的编译常量的一个问题
2009-06-04 14:22 1520问题是:有两个JAVA文件,第一个里面放的是常量,第二个 ...
相关推荐
本篇文章将深入探讨如何在Java中使用FreeMarker生成带有盖章的PDF合同文件。 首先,让我们了解FreeMarker的基本概念。FreeMarker是一个基于模板的语言,它与Java代码分离,允许开发者用简单的模板语法来表示数据。...
通过以上步骤,你可以在SpringBoot应用中成功集成并使用Freemarker模板引擎。这只是一个基础的介绍,实际开发中,你可以根据项目需求配置更多的Freemarker特性和功能,如缓存管理、日期格式化等,以满足复杂的应用...
标题中的“grails使用freemarker.rar”表明这是一个关于如何在Grails框架中应用FreeMarker模板引擎的资源包。FreeMarker是一个开源的、基于Java的模板引擎,它用于生成动态HTML或其他格式的文本,比如XML、PDF等。...
要开始在Struts2中使用FreeMarker模板,首先需要将`freemarker-2.3.8.jar`库文件导入到你的项目的`WEB-INF/lib`目录下。这个库包含了FreeMarker模板引擎的所有必要组件。然后创建一个新的web工程,例如`...
在Struts2中使用FreeMarker,主要涉及以下几个核心概念和步骤: 1. **配置FreeMarker**: - 在Struts2的配置文件(通常为struts.xml)中,你需要指定FreeMarker作为默认的视图技术。 - 配置FreeMarker的路径,...
Springboot项目中: 1. 使用Apache POI 3.9 自定义样式导出Excel文件...2. 使用freemarker动态生成word .doc文档(带图片Word以及复杂格式word) 详细说明见个人博客及 github: https://github.com/DuebassLei/excel-poi
Java 使用 FreeMarker 模板技术导出 Word 是一种常见的数据动态生成文档的方法,它结合了 Java 的编程能力和 FreeMarker 模板引擎的强大功能,能够帮助开发者高效地生成结构化的 Word 文档。FreeMarker 是一个开源的...
下面我们将深入探讨如何配置Spring以使用Freemarker作为视图解析器,以及如何在前端页面上全面使用Freemarker进行渲染。 首先,我们需要在Spring的配置文件中添加对Freemarker的支持。这通常涉及到以下几个步骤: ...
1. **模板语言**:Freemarker使用简单的文本模板语言,不依赖任何编程语言。模板中包含控制结构(如条件语句、循环)和数据引用表达式。 2. **分离关注点**:Freemarker的核心理念是将逻辑和展示分开,开发者负责...
本话题将深入探讨如何使用FreeMarker扩展Struts2的标签库,以增强视图层的表现力和灵活性。 首先,FreeMarker是一个基于模板的语言,它允许开发者用简单的语法来处理数据模型并生成HTML或其他文本。在Struts2中,...
7. **与Spring Framework集成**:在Spring MVC中,Freemarker可以作为视图解析器使用,通过配置`FreeMarkerConfigurer`和`FreeMarkerViewResolver`,可以轻松地将Freemarker与Spring的控制器层结合。 8. **模板继承...
FreeMarker使用${...}表达式来插入变量,#{...}用于输出注释,以及、等控制结构进行条件判断和循环。然而,这些默认标签可能无法满足所有复杂的场景,因此自定义标签就显得尤为必要。 自定义FreeMarker标签通常涉及...
总的来说,“freemarker导出doc及docx”涉及到了Freemarker模板引擎的使用、SpringBoot的集成、以及利用Apache POI等库处理Word文档的技巧。实际操作时,你需要根据项目需求定制模板,设置数据模型,最后通过编程...
这篇博客文章“使用FreeMarker生成java代码”深入探讨了如何利用FreeMarker来自动化Java代码的生成过程,从而提高开发效率。 FreeMarker的工作原理是将设计模式(模板)与数据模型结合,模板中包含了一系列控制结构...
本示例将详细讲解如何使用Freemarker来导出Word文档,并提供供他人下载的功能。 首先,理解Freemarker的核心概念。Freemarker是一个基于模板的Java库,它与后台数据模型结合,用于生成输出文本。模板是包含动态内容...
这个"jdk1.8下可以使用的freemarker.jar"文件是专门为Java 8环境优化的Freemarker库,使得开发者能够在Java 8平台上无缝地集成和使用Freemarker进行动态内容渲染。 Freemarker的核心概念是模板(Template),它是一...
在代码中,使用FreeMarker API加载模板文件,创建`Template`对象,然后用数据模型实例化`Map`对象。通过`Configuration`对象的`process`方法,将模板与数据模型合并,生成Word文档的字节流。最后,你可以选择将这个...
在这个场景下,使用Freemarker模板技术可以帮助我们实现这一目标。Freemarker是一个强大的、轻量级的模板引擎,它可以将模板和数据模型结合起来,生成输出文本,如HTML、XML或者如本例中的PDF。 首先,我们需要了解...
### Freemarker 使用总结与详解 #### 一、Freemarker 概述 Freemarker 是一种用于生成动态页面的模板引擎。它不依赖任何 Web 容器,可以在任何 Java 应用程序中使用。Freemarker 的核心优势在于其简单易学且功能...
本篇将深入探讨如何使用FreeMarker来生成一套完整的MVC流程,包括Controller、Service、Impl、PageModel、DAO以及Mapper。 1. **FreeMarker简介** FreeMarker是一个基于模板的开源Java库,用于生成文本输出。它的...