Is there a way to detect a number (java double) being set to NaN in a Freemarker template?
Basically I’d like to do something like:
<#if val?is_nan>
-
<#else>
${val}
</#if>
I tried to convert to string and then check for the \uFFFD character, but fail to do the correct compare here.
I have the impression that my problems come from the way, I give the data to the processing
Map<String, Object> root = new HashMap<String, Object>();
root.put("var", objectToRender);
template.process(root, out);
Where objectToRender is the data structure I use. Perhaps I need to set some special flag for double handling?
Update: Starting from FreeMarker 2.3.20 you can just write
val?is_nan. For older versions, see below…There’s no
n?is_nan, but you can create your own method that you can use asisNaN(n):Put
IsNaNMethod.INSTANCEinto the data-model as “isNaN” (or into all data-models withconfig.setSharderVariable), or just pull it in in an#include-d/#import-ed template with<#assign isNaN = "com.example.IsNaNMethod"?new()>.