freemarker中处理页面自动四舍五入问题

在开发中很容易忽视一点,输入一个值(可能是小数),输出时如果不做处理,就很容易出现 

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

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

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

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

<#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%  

=======================================================================

<#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%  

=======================================================================
<#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  

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

outputs this: 

1.23  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值