使用freemarker中的小数点处理

本文主要介绍了在使用Freemarker时遇到的小数点处理问题,当从数据库取出带有小数的数值并直接输出时,可能会丢失小数部分。解决办法是使用`${x?if_exists?string.number}`或者`${x?if_exists.toString()?html}`。同时,文章还详细阐述了Freemarker中`?c`、`?string`等内置函数对于数字转换的规则和用法,包括不同格式化选项如`number`、`currency`、`percent`,以及自定义格式和各种取整方法。
摘要由CSDN通过智能技术生成

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值