tags:

views:

440

answers:

3

How to check for Double.NaN in JSP with JSTL tags?

A: 

Is something like this:

<c:if test="$variable eq Double.NaN">

what you're looking for?

It'd be nice if Java constants were "visible" in EL, JSTL however doesn't work with them.

Workaround could be to put Double.NaN into scope of JSTL (e.g. put it to applicatioScope by making it a servlet attribute) programatically.

There also is a Jakarta tag library doing this: http://jakarta.apache.org/taglibs/sandbox/doc/unstandard-doc/intro.html (see useConstant tag). However, I've never used that, and the library itself seem to be in sandbox for ages. But it can at least give an idea of how to do implement this :)

david a.
Nan never equals Nan in any language.
leppie
A: 

The "NaN" is just outputted as a String. So

<c:if test="${variable == 'NaN'}">

should do.

BalusC
+2  A: 

One thing to look out for when working with Double.NaN is that Double.NaN (I think it is even in the IEEE spec) is supposed to compare as not equal to everything including NaN.

Hence, the only way to properly check if a number is NaN (apart from creating a String out of it) is to see if the value != value. JSTL is not my cup of tea but I guess it is valid there as well.

Read more in Wikipedia

Fredrik
This is probably the answer I should test. However, I solved the problem by putting a function in Java (that uses Double.isNan()) and using that function in JSTL, so I've moved on.If someone shows this answer works, I'll accept it. Thanks.
dfrankow
I can confirm that this works - if the value is NaN, value == value will return false.
Glenn Barnett