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 3000000 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 four predefined number formats: computer, currency, number, and percent. The exact meaning of these is locale (nationality) specific, and is controlled by the Java platform installation, rather than by FreeMarker, except for computer, which uses the same formatting as the c built-in. You can use these predefined formats like this:
|
|||
If your locale is US English, this will certainly produce:
|
|||
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:
|
|||
Will now output:
|
|||
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:
|
|||
outputs this:
|
|||
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.
Apart 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:
|
|||
outputs this:
|
|||
Note that the number formatting is locale sensitive:
|
|||
outputs this:
|
|||
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:
|
|||
Prints:
|
|||
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.
相关推荐
赠送jar包:freemarker-2.3.31.jar; 赠送原API文档:freemarker-2.3.31-javadoc.jar; 赠送源代码:freemarker-2.3.31-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.31.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.30.jar; 赠送原API文档:freemarker-2.3.30-javadoc.jar; 赠送源代码:freemarker-2.3.30-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.30.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.30.jar; 赠送原API文档:freemarker-2.3.30-javadoc.jar; 赠送源代码:freemarker-2.3.30-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.30.pom; 包含翻译后的API文档:...
FreeMarkerIDE-1.0.0.GA.zip是一款专为FreeMarker设计的Eclipse插件,旨在提升开发人员构建和管理FreeMarker应用的效率。FreeMarker是一个强大的模板引擎,广泛应用于Java环境中,用于生成动态HTML或其他格式的文本...
camel-freemarker-1.6.4.jar, camel-freemarker-2.8.1.jar, com.springsource.freemarker-2.3.15.jar, com.springsource.freemarker-sources-2.3.15.jar, freemarker-1.4.1.jar, freemarker-2-3-18.jar, freemarker-...
JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-2.3.8JavaEE源代码 freemarker-...
赠送jar包:freemarker-2.3.23.jar; 赠送原API文档:freemarker-2.3.23-javadoc.jar; 赠送源代码:freemarker-2.3.23-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.23.pom; 包含翻译后的API文档:...
`freemarker-ide-0.9.14` 是一个针对Freemarker模板语言的集成开发环境插件,旨在提升在MyEclipse中的开发效率和体验。 这个插件版本为0.9.14,可能包含了对Freemarker语法的高亮显示、代码自动完成、错误检查、...
在这个"freemarker-2.3.22"的压缩包中,包含了Freemarker 2.3.22版本的相关资源,有助于我们深入了解和使用这个库。 首先,`README.txt`通常包含项目的基本信息、安装指南和快速入门等内容。在这个版本中,它可能...
赠送jar包:freemarker-2.3.20.jar; 赠送原API文档:freemarker-2.3.20-javadoc.jar; 赠送源代码:freemarker-2.3.20-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.20.pom; 包含翻译后的API文档:...
本压缩包"freemarker-ide-0.9.14.rar"提供的是一个针对IDE的Freemarker插件,特别适用于MyEclipse 6.5版本。 该插件名为"Hudson Freemarker IDE",版本为0.9.14,它增强了MyEclipse对Freemarker模板语言的支持。在...
这个"apache-freemarker-2.3.26-incubating-bin.tar.gz"文件是Apache FreeMarker的一个特定版本,即2.3.26,处于孵化器阶段(incubating)。它包含了FreeMarker库的二进制文件和相关的文档,为开发者提供了完整的...
在本Demo中,我们将深入探讨如何将FreeMarker模板引擎整合进SSM框架,以及PageHelper分页插件的使用。 首先,让我们来了解一下SSM框架的核心组件: 1. **Spring**:这是一个全面的Java应用框架,提供依赖注入(DI...
赠送jar包:freemarker-2.3.20.jar; 赠送原API文档:freemarker-2.3.20-javadoc.jar; 赠送源代码:freemarker-2.3.20-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.20.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.23.jar; 赠送原API文档:freemarker-2.3.23-javadoc.jar; 赠送源代码:freemarker-2.3.23-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.23.pom; 包含翻译后的API文档:...
赠送jar包:freemarker-2.3.31.jar; 赠送原API文档:freemarker-2.3.31-javadoc.jar; 赠送源代码:freemarker-2.3.31-sources.jar; 赠送Maven依赖信息文件:freemarker-2.3.31.pom; 包含翻译后的API文档:...
这个“freemarker-2.3.23-中文手册.zip”包含了Freemarker 2.3.23版本的详尽中文文档,对于学习和掌握Freemarker的使用非常有帮助。 在Freemarker 2.3.23版中,我们首先会接触到它的核心概念,包括模板(Template)...
这个"freemarker-2.3.28.jar"是Freemarker库的一个具体版本,版本号为2.3.28,它是Java的一个可执行的JAR(Java Archive)文件,用于在Eclipse集成开发环境中作为插件使用。 在Freemarker的2.3.28版本中,我们可以...
【标题】"FreeMarkerDemo-java.rar" 是一个与Spring Boot相关的示例项目,它演示了如何将Word文档(docx格式)转换为FreeMarker(ftl)模板。这个压缩包包含了一个Java应用,该应用利用Spring Boot的强大功能,以及...