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 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

Related Questions

is there a better (more elegant way - in datepicker itself maybe?) way to
Is there anything more compact (or otherwise better) than this? <x:Array x:Key=titles Type=System:String> <System:String>Mr.</System:String>
Which way is better for removing float decimals places or is there a more
Is there better way to enumerate all photos on the device than this one?
Is there a better way to format data uniformly than to store data as
Just wanted to check to see if there's a more elegant way to accomplish
So yeah, I'm just trying to see if there is a more elegant way
Is there better use of available storage space and faster network access using the
Is there better way to delete a parameter from a query string in a
Is there a better way to code this? def __contains__(self, e): return e in

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.