`
gongstring
  • 浏览: 588115 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

使用freemarker中的小数点处理的一点心得!

    博客分类:
  • Java
阅读更多

今天在项目中发现从界面使用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.

分享到:
评论
1 楼 xiaobojava 2009-12-04  
受教了!

相关推荐

    freemarker中文手册,轻松掌握!!

    Freemarker是一个强大的模板引擎,常用于Web应用中的视图层开发,比如Java Web项目。它允许开发者使用简单的模板语言来动态生成HTML或其他文本格式的文档。本手册将帮助你全面理解和熟练运用Freemarker。 1. **...

    Java中使用 FreeMarker 生成pdf盖章合同文件

    本篇文章将深入探讨如何在Java中使用FreeMarker生成带有盖章的PDF合同文件。 首先,让我们了解FreeMarker的基本概念。FreeMarker是一个基于模板的语言,它与Java代码分离,允许开发者用简单的模板语法来表示数据。...

    struts2中使用freeMarker

    在Struts2中使用FreeMarker,主要涉及以下几个核心概念和步骤: 1. **配置FreeMarker**: - 在Struts2的配置文件(通常为struts.xml)中,你需要指定FreeMarker作为默认的视图技术。 - 配置FreeMarker的路径,...

    在struts2中使用freemarker模版

    要开始在Struts2中使用FreeMarker模板,首先需要将`freemarker-2.3.8.jar`库文件导入到你的项目的`WEB-INF/lib`目录下。这个库包含了FreeMarker模板引擎的所有必要组件。然后创建一个新的web工程,例如`...

    freemarker 中文乱码解决

    在处理Freemarker模板引擎时,中文乱码问题是一个常见的挑战,尤其是在国际化应用中。Freemarker是一款功能强大的模板引擎,被广泛应用于Web开发中,用于动态生成HTML、XML等文本格式的页面。然而,当涉及到非英文...

    freemarker中文文档与包

    10. **最佳实践**: 使用Freemarker时,应遵循最佳实践,如保持模板简洁,避免过多的逻辑处理,使用合适的模板命名约定,以及充分利用模板继承和导入功能以减少重复代码。 这个“freemarker中文文档与包”资源将帮助...

    grails使用freemarker.rar

    总结来说,这个压缩包内容可能包括如何在Grails项目中配置和使用FreeMarker,以及如何处理领域类之间的关系映射,特别是“one2many”关系。通过学习这些知识,开发者可以更有效地构建Grails应用,并利用FreeMarker...

    FreeMarker中文文档.pdf下载

    字符串处理是FreeMarker中的一个重要方面,可以进行字符串的拼接、截取、替换等操作。例如: ```ftl ${str?upper_case} &lt;!-- 输出 "HELLO WORLD" --&gt; ${str?replace("o", "0")} &lt;!-- 输出 "Hell0 W0rld" --&gt; ``` ...

    freemarker中文API手册

    FreeMarker是一种模板引擎,用于生成文本输出,使用纯Java编写,特别适合基于MVC模式的应用程序。FreeMarker提供了强大的模板语言,可以生成各种文本,如HTML、XML、RTF、Java源代码等等。FreeMarker也支持插件式...

    springboot如何使用Freemarker模版引擎

    通过以上步骤,你可以在SpringBoot应用中成功集成并使用Freemarker模板引擎。这只是一个基础的介绍,实际开发中,你可以根据项目需求配置更多的Freemarker特性和功能,如缓存管理、日期格式化等,以满足复杂的应用...

    springboot中使用freemarker动态生成word文档,以及使用POI导出自定义格式Excel

    Springboot项目中: 1. 使用Apache POI 3.9 自定义样式导出Excel文件...2. 使用freemarker动态生成word .doc文档(带图片Word以及复杂格式word) 详细说明见个人博客及 github: https://github.com/DuebassLei/excel-poi

    FreeMarker2.3.23官方中文文档

    通过阅读和理解FreeMarker 2.3.23的官方中文文档,开发者能够熟练掌握FreeMarker的使用技巧,从而在Web应用开发中更加高效地创建和管理动态内容。这份文档详尽地涵盖了FreeMarker的各个方面,无论是初学者还是经验...

    FreeMarker中文参考手册以及jar包

    在Java项目中,通常将这个jar包添加到类路径(classpath)中,以便在程序中使用FreeMarker的API创建和处理模板。 总的来说,这份资源提供了全面的学习FreeMarker的资料,无论是初学者还是经验丰富的开发者,都能...

    freemarker中文学习资料

    - **Spring MVC**:在Spring MVC中,Freemarker作为视图解析器,处理Controller返回的模型数据。 - **配置设置**:通过`freemarkerConfigurer` bean,可以在Spring中配置Freemarker的属性。 7. **错误处理和调试*...

    Freemarker中遍历list集合实例

    在这个例子中,`users`是Java端传递到Freemarker模板的一个list,`user`是循环中的变量,每次迭代时代表`users`列表中的一个元素。`as`关键字用于定义迭代变量。在循环体内,我们可以通过`user`访问当前元素的属性,...

    非常好的Freemarker中文教程

    Freemarker是一款强大的模板引擎,常...通过详细研读这份教程,开发者不仅可以掌握Freemarker的基本使用,还能深入理解其高级特性和优化技巧,从而在实际工作中更高效地生成静态文件,提升Web应用的性能和用户体验。

    EOS 中使用freemarker模板生成PDF文件导出

    在博文《EOS中使用Freemarker模板生成PDF文件导出》中,作者可能详细介绍了以上步骤,并可能分享了具体代码示例和遇到的问题解决方案。由于没有直接提供该博文的内容,我们只能推测其可能涵盖的主题。例如,作者可能...

    freemarker 自定义freeMarker标签

    这通常是通过`Configuration`对象的`setSharedVariable`方法完成的,将你的自定义指令类实例绑定到一个特定的名字,这个名字就是你在模板中使用的标签名。 3. 使用自定义标签:在模板文件中,你可以像使用内置标签...

    FreeMarker+中文手册

    - **异常处理**:`&lt;#try&gt;`、`&lt;#catch&gt;`、`&lt;#finally&gt;`用于捕获和处理模板执行过程中的异常。 FreeMarker中文手册中会详细解释这些概念和语法,并提供丰富的示例帮助理解和应用。通过深入阅读和实践,你可以掌握...

Global site tag (gtag.js) - Google Analytics