E.S. E.S. - 1 year ago 94
Java Question

How do I check if a string is convertable to a number

In freemarker, I have a macro

that does the following:

<#macro myfunc x>
<#assign x=(x?number)>

So, essentially, if
(which starts off as a string) and is convertible to a number, then I want to wrap it in
otherwise just display the value.

I am fine with this solution except for the ugly
error logging which comes with it.

There does not seem to be a simple way to check if a string is convertible to a number without using
or my research is faltering -- I would also rather not do this on the Java side because I see this as view code and doesn't belong with my model code. Anyone know?

Answer Source

You could use regex:

<#if x?matches("\\d+")>
    // do something with x?number
    // do something with x
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download