freemarker常用的值格式化方法:
1、${price?string('0.00')}
对price进行格式化,小数点后不足2位用0补足。
比如:price=1
输出:1.00
2、${price?string('#.##')}
对price进行格式化,小数点后多余2位只保留2位,少于2位显示实际位数。
比如:price=1.234
输出:1.23
3、${price?string.currency}
对price进行格式化,显示为货币格式。
比如:price=1
输出:¥1.00
4、${price?string.percent}
对price进行格式化,显示为百分比。
比如:price=1
输出:100%
1.if-else常用
<#if signStatus??> //如果signStatus值存在
<#if signStatus=='2'>是<#else>否</#if> <#else> 否 </#if>
2.时间格式化
时间戳转日期
${time?number_to_datetime}
${time?number*1000} 时间戳乘1000
日期格式化
${dateTime?string('yyyy-MM-dd hh:mm:ss')} 格式化为 2020-11-12 15:05:29
${dateTime?string('yyyy-MM-dd ')} 格式化为 2020-11-12
3.数值格式化
数值如果有小数保留两位小数,否则补0两位,例如128.81, 688则为 688.00
${((totalInvoice.amount)!0)?string('###,##0.00')}
另外,
1 ${num?string('0.00')}
2 如果小数点后不足两位,用 0 代替
3
4 ${num?string('#.##')}
5 如果小数点后多余两位,就只保留两位,否则输出实际值
6 输出为:1239765.46
7
8 ${num?string(',###.00')}
9 输出为:1,239,765.46
10 整数部分每三位用 , 分割,并且保证小数点后保留两位,不足用 0 代替
11
12 ${num?string(',###.##')}
13 输出为:1,239,765.46
14 整数部分每三位用 , 分割,并且小数点后多余两位就只保留两位,不足两位就取实际位数,可以不不包含小数点
15
16 ${num?string('000.00')}
17 输出为:012.70
18 整数部分如果不足三位(000),前面用0补齐,否则取实际的整数位
19
20 ${num?string('###.00')}
21 等价于
22 ${num?string('#.00')}
23 输出为:12.70
参考https://blog.youkuaiyun.com/qq_32534855/article/details/67631788
4.list迭代
1 <#list 0..(bigTypeList!?size-1) as i>
2 <#if bigTypeList[i].code==settleCheckBillVO.bigType>
3 ${bigTypeList[i].name}
4 </#if>
5 </#list>
5.数值为空导致的报错的处理方式
添加以下红色的部分,当为对象时需要加括号(一些可能为空的数据需要这么处理)
${saleUnifiedIdentityCode!""}
${((totalInvoice.amount)!0)?string('###,##0.00')}
1 <#if receiptVOList?size != 0>
2 <div style="font-size: 16px;
3 font-weight: bold;padding:10px 0">匹配通过收据明细 <span style="margin-left: 20px">共${(receiptVOList)?size!0}张</span></div>
4 <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tb">
5 <tr>
6 <td width="16%" class="tl" style="border-width: 1px 1px 1px 1px">发票类型</td>
7 <td width="10%" class="tc" style="border-width: 1px 1px 1px 0">开票日期<i></i></td>
8 <td width="12%" class="tc" style="border-width: 1px 1px 1px 0">收据编号<i></i></td>
9 <td width="12%" class="tc" style="border-width: 1px 1px 1px 0">含税金额<i></i></td>
10 <td width="26%" class="tc" style="border-width: 1px 1px 1px 0">备注<i></i></td>
11 </tr>
12
13 <#if receiptVOList?size != 0>
14 <#list 0..(receiptVOList!?size-1) as i>
15 <tr class="<#if i%2==0>space</#if>"
16 style=" <#if (receiptVOList[i].totalAmount<0)>color: red</#if>">
17 <td class="tl" style="border-width: 0px 1px 1px 1px"><p>收据</p></td>
18 <td class="tc" style="border-width: 0px 1px 1px 0px">
19 <p>${(receiptVOList[i].openInvoiceTime?number_to_datetime)?string('yyyy-MM-dd')!}</p></td>
20 <td class="tc" style="border-width: 0px 1px 1px 0px"><p>${receiptVOList[i].billNo!}</p></td>
21 <td class="tc" style="border-width: 0px 1px 1px 0px">
22 <p>${((receiptVOList[i].totalAmount)!0)?string('###,##0.00')}</p></td>
23 <td class="tc" style="border-width: 0px 1px 1px 0px"><p>${(receiptVOList[i].remark)!}</p></td>
24 </tr>
25 </#list>
26 </#if>
27 </table>
28 </#if>
6.字符串操作函数(参考下面,即可大概清楚ftl中函数如何使用)
${“strabg”?replace(“ab”,”in”)} 结果为string
${“string”?contains(“ing”)?string} 结果为true
注意:布尔值必须转换为字符串才能输出
${“string”?index_of(“in”) 结果为3
${“string”?index_of(“ab”) 结果为-1
length返回字符串的长度 ${“string”?length}结果为6
lower_case将字符串转为小写
${“STRING”?lower_case}à结果为string
upper_case将字符串转为大写
${“string”?upper_case}à结果为STRING
ends_with 判断某个字符串是否由某个子串结尾,返回布尔值。
${“string”?ends_with(“ing”)?string} 返回结果为true
本文介绍了Freemarker中常见的值格式化方法,如价格格式化、货币、百分比显示,以及if-else条件判断、时间戳和日期转换。还涵盖了列表迭代和空值处理技巧,以及字符串操作函数的使用实例。
995

被折叠的 条评论
为什么被折叠?



