在开发中很容易忽视一点,输入一个值(可能是小数),输出时如果不做处理,就很容易出现
隐形的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}
${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}
${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