Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:07:15+00:00 2026-06-05T10:07:15+00:00

Is there a way to detect a number (java double) being set to NaN

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:07:17+00:00Added an answer on June 5, 2026 at 10:07 am

    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 as isNaN(n):

    import java.util.List;
    
    import freemarker.template.TemplateBooleanModel;
    import freemarker.template.TemplateMethodModelEx;
    import freemarker.template.TemplateModelException;
    import freemarker.template.TemplateNumberModel;
    
    public class IsNaNMethod implements TemplateMethodModelEx {
    
        public static final IsNaNMethod INSTANCE = new IsNaNMethod();
    
        public Object exec(@SuppressWarnings("rawtypes") List args)
        throws TemplateModelException {
            if (args.size() != 1) {
                throw new TemplateModelException("isNaN needs exactly 1 arguments!");
            }
    
            Object arg = args.get(0);
    
            if (arg == null) {
                throw new TemplateModelException(
                        "The argument to the isNaN method must not be null!");
            }
    
            if (!(arg instanceof TemplateNumberModel)) {
                throw new TemplateModelException(
                        "The argument to the isNaN method must be a number! " +
                        "(The class of the value was: " + arg.getClass().getName() + ")");
            }
    
            Number n = ((TemplateNumberModel) arg).getAsNumber();
            if (n instanceof Double) {
                return ((Double) n).isNaN()
                        ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
            } else if (n instanceof Float) {
                return ((Float) n).isNaN()
                        ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
            } else {
                return TemplateBooleanModel.FALSE;
            }
        }
    
    }
    

    Put IsNaNMethod.INSTANCE into the data-model as “isNaN” (or into all data-models with config.setSharderVariable), or just pull it in in an #include-d/#import-ed template with <#assign isNaN = "com.example.IsNaNMethod"?new()>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a platform independent way to detect the number of physical and/or virtual
Is there a way for a Java program to detect when the operating system
Is there a way to detect if a value is a number in a
In Java, is there an elegant way to detect if an exception occurred prior
Is there a way to detect the number of arguments a function in a
Is there any way to detect when google earth plugin has finished installing so
Is there any way to detect in javascript, if the following html5 or css
Is there a way to detect whether sys.stdout is attached to a console terminal
Is there any way to detect when an app is no longer active? That
Is there a way to detect when a UITextView has finished scrolling? As a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.