freemarker类型转换与if判断,字符串转数字,数字转字符串,布尔值转换为字符串,字符串和日期转换

您所在的位置:网站首页 数字字母转换谜题 freemarker类型转换与if判断,字符串转数字,数字转字符串,布尔值转换为字符串,字符串和日期转换

freemarker类型转换与if判断,字符串转数字,数字转字符串,布尔值转换为字符串,字符串和日期转换

#freemarker类型转换与if判断,字符串转数字,数字转字符串,布尔值转换为字符串,字符串和日期转换| 来源: 网络整理| 查看: 265

一、前言

freemarker中也是根据类型进行判断的,先看下如下的判断:

 

·相等

classify是一个对象,其中的id是Long类型,在页面上被转成了string,运行会报如下的错误:

 

[11:02:48,694][ERROR] freemarker.runtime:60 - Error executing FreeMarker template FreeMarker template error: Can't compare values of these types. Allowed comparisons are between two numbers, two strings, two dates, or two booleans. Left hand operand is a string (wrapper: f.t.SimpleScalar). Right hand operand is a number (wrapper: f.t.SimpleNumber). The blamed expression: ==> classify.id == 1 [in template "modules/front/classify/classify.html" at line 87, column 74]

错误信息提示if左侧是一个字符串,右侧是一个数值型的,所以不通过,这个时候就需要类型转换,如下方式即可通过:

·相等

 

二、类型转换

 

一些常见的转换方式如下:

 

2.1、字符串转化为数字 ${'300.001'?number} 结果为:300

如果用if进行判断的话,如下方式结果为不相等

相等 不相等

右边和左边一样才相等

相等

 

2.2、数字转化为字符串

 

有以下两种方式,使用?c和?string进行转换

${300.001?c} 结果为:300.001 string(format) format:数字格式


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3