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 3344312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:03:17+00:00 2026-05-18T01:03:17+00:00

I recently ran into a piece of code very much like this one: var

  • 0

I recently ran into a piece of code very much like this one:

var nHours = parseInt(txtHours);
if( isNaN(nHours))  // Do something
else // Do something else with the value

The developer who wrote this code was under the impression that nHours would either be an integer that exactly matched txtHours or NaN. There are several things wrong with this assumption.

First, the developer left of the radix argument which means input of "09" would result in a value of 0 instead of 9. This issue can be resolved by adding the radix in like so:

var nHours = parseInt(txtHours,10);
if( isNaN(nHours))  // Do something
else // Do something else with the value

Next, input of "1.5" will result in a value of 1 instead of NaN which is not what the developer expected since 1.5 is not an integer. Likewise a value of "1a" will result in a value of 1 instead of NaN.

All of these issues are somewhat understandable since this is one of the most common examples of how to convert a string to an integer and most places don’t discuss these cases.

At any rate it got me thinking that I’m not aware of any built in way to get an integer like this. There is Number(txtHours) (or +txtHours) which comes closer but accepts non-integer numbers and will treat null and "" as 0 instead of NaN.

To help the developer out I provided the following function:

function ConvertToInteger(text)
{
    var number = Math.floor(+text);
    return text && number == text ? number : NaN;
}

This seems to cover all the above issues. Does anyone know of anything wrong with this technique or maybe a simpler way to get the same results?

  • 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-05-18T01:03:18+00:00Added an answer on May 18, 2026 at 1:03 am

    Here, that’s what I came up with:

    function integer(x) {
        if (typeof x !== "number" && typeof x !== "string" || x === "") {
            return NaN;
        } else {
            x = Number(x);
            return x === Math.floor(x) ? x : NaN;
        }
    }
    

    (Note: I updated this function to saveguard against white-space strings. See below.)

    The idea is to only accept arguments which type is either Number or String (but not the empty string value). Then a conversion to Number is done (in case it was a string), and finally its value is compared to the floor() value to determine if the number is a integer or not.

    integer(); // NaN
    integer(""); // NaN
    integer(null); // NaN
    integer(true); // NaN
    integer(false); // NaN
    integer("1a"); // NaN
    integer("1.3"); // NaN
    integer(1.3); // NaN    
    integer(7); // 7
    

    However, the NaN value is “misused” here, since floats and strings representing floats result in NaN, and that is technically not true.

    Also, note that because of the way strings are converted into numbers, the string argument may have trailing or leading white-space, or leading zeroes:

    integer("   3   "); // 3    
    integer("0003"); // 3
    

    Another approach…

    You can use a regular expression if the input value is a string.
    This regexp: /^\s*(\+|-)?\d+\s*$/ will match strings that represent integers.

    UPDATED FUNCTION!

    function integer(x) {
        if ( typeof x === "string" && /^\s*(\+|-)?\d+\s*$/.test(x) ) {
            x = Number(x);
        }
        if ( typeof x === "number" ) {
            return x === Math.floor(x) ? x : NaN;
        }
        return NaN;
    }
    

    This version of integer() is more strict as it allows only strings that follow a certain pattern (which is tested with a regexp). It produces the same results as the other integer() function, except that it additionally disregards all white-space strings (as pointed out by @CMS).

    Updated again!

    I noticed @Zecc’s answer and simplified the code a bit… I guess this works, too:

    function integer(x) {
        if( /^\s*(\+|-)?\d+\s*$/.test(String(x)) ){
            return parseInt(x, 10);
        }
        return Number.NaN;
    }  
    

    It probaly isn’t the fastest solution (in terms of performance), but I like its simplicity 🙂

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

Sidebar

Related Questions

I'm writing a C parser using PLY, and recently ran into a problem. This
Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's
I have recently ran into this strange issue, I was trying to reference parent
I ran into a problem recently and found out that this is a bona-fide
I recently ran into this problem: Rails 3 link_to (:method => :delete) not working
Just ran into this recently and I thought it'd be helpful to share. The
I recently ran into a problem that I thought boost::lambda or boost::phoenix could help
I recently ran into a issue where intermediate link betweeen a TCP server and
I recently ran into a problem with a COM object that was using a
A co-worker recently ran into a situation where a query to look up security

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.