Freemarker(FTL)-如何检查字符串是否可以使用?number转换为数字 [英] Freemarker (FTL) - How to check if String can be converted to a number using ?number
本文介绍了Freemarker(FTL)-如何检查字符串是否可以使用?number转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查字符串是否可以转换为数字?
How can I check if a string can be converted to a number?
例如,除非 SelfAge
是要转换的有效字符串,否则这将不起作用:
For example this will not work unless SelfAge
is a valid String to convert:
<#if SelfAge?? && (SelfAge?number > 15)>
测试数据:
SelfAge = "October 30, 1913 // "Can't convert this string to number: "October 30, 1913"
SelfAge = "User-submitted-comment" // Can't convert this string to number: "User-submitted-comment"
SelfAge = "1/12" // Can't convert this string to number: "1/12"
推荐答案
此方法适用于非负整数:
This works for non-negative integers:
[#if ageString?matches("^\\d+$")]
[#assign ageNumber = ageString?number]
[/#if]
这篇关于Freemarker(FTL)-如何检查字符串是否可以使用?number转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文