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

  • Home
  • SEARCH
  • 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 85051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:01:42+00:00 2026-05-10T22:01:42+00:00

Is there a better, more elegant (and/or possibly faster) way than boolean isNumber =

  • 0

Is there a better, more elegant (and/or possibly faster) way than

boolean isNumber = false; try{    Double.valueOf(myNumber);    isNumber = true; } catch (NumberFormatException e) { } 

…?


Edit: Since I can’t pick two answers I’m going with the regex one because a) it’s elegant and b) saying ‘Jon Skeet solved the problem’ is a tautology because Jon Skeet himself is the solution to all problems.

  • 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. 2026-05-10T22:01:43+00:00Added an answer on May 10, 2026 at 10:01 pm

    I don’t believe there’s anything built into Java to do it faster and still reliably, assuming that later on you’ll want to actually parse it with Double.valueOf (or similar).

    I’d use Double.parseDouble instead of Double.valueOf to avoid creating a Double unnecessarily, and you can also get rid of blatantly silly numbers quicker than the exception will by checking for digits, e/E, – and . beforehand. So, something like:

    public boolean isDouble(String value) {             boolean seenDot = false;     boolean seenExp = false;     boolean justSeenExp = false;     boolean seenDigit = false;     for (int i=0; i < value.length(); i++)     {         char c = value.charAt(i);         if (c >= '0' && c <= '9')         {             seenDigit = true;             continue;         }         if ((c == '-' || c=='+') && (i == 0 || justSeenExp))         {             continue;         }         if (c == '.' && !seenDot)         {             seenDot = true;             continue;         }         justSeenExp = false;         if ((c == 'e' || c == 'E') && !seenExp)         {             seenExp = true;             justSeenExp = true;             continue;         }         return false;     }     if (!seenDigit)     {         return false;     }     try     {         Double.parseDouble(value);         return true;     }     catch (NumberFormatException e)     {         return false;     } } 

    Note that despite taking a couple of tries, this still doesn’t cover ‘NaN’ or hex values. Whether you want those to pass or not depends on context.

    In my experience regular expressions are slower than the hard-coded check above.

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

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer To your questions: How many threads does the OS generate… May 11, 2026 at 3:01 pm
  • added an answer I changed the ArrayList to List<Students>, and: List<SourceData> source =… May 11, 2026 at 3:01 pm
  • added an answer There's no reason not to add a web service project.… May 11, 2026 at 3:01 pm

Related Questions

I'm working on a site where a user could select certain dates that apply
This question is motivated by something I've lately started to see a bit too
I was thinking earlier today about an idea for a small game and stumbled
I have an Address object that has properties AddressLine1, AddressLine2, Suburb, State, ZipCode. (there

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.