tags:

views:

551

answers:

6

Double.TryParse returns a value, and I don't need a value. I need to be able to tell if a string is numeric and just return a bool.

is there a way to do this?

+5  A: 

One way is to add a reference to Microsoft.VisualBasic and then use Information.IsNumeric().

using Microsoft.VisualBasic;

...

if (Information.IsNumeric("1233434.0"))
{
    Console.WriteLine("Yes");
}
Mitch Wheat
I was actually considering this but I think I would have to add that extra assembly and that's hefty.
Sara Chipps
You mean the extra assembly that's already part of the .NET framework?
Mitch Wheat
It wouldn't still add to the size of my application?
Sara Chipps
provided the .NET framework is already installed...
Mitch Wheat
+8  A: 

Well, you could use a regular expression, but why not just discard the value from Double.TryParse and move on? I don't think it will be worth the effort trying to duplicate this code.

Lasse V. Karlsen
+1. That's what I usually do. I added the VB way as an alternative.
Mitch Wheat
because I'm posting my code on my blog and the last thing I need is some uber-nerd saying "well, why didn't you..." :)
Sara Chipps
Dev is all about learning)So if some uber-nerd comes up just give him the kudos and update the code snippet)
Rinat Abdullin
Note that Double.TryParse will handle things like cultural differences (thousand separators, different decimal separators), exponential values (like 1E+05), etc. If you want to support all that, you really don't have much choice. If you're only interested in integers, then ... Int32.TryParse :)
Lasse V. Karlsen
+1  A: 

How about a regular expression?

string s = "13.2";
bool bIsDecimal = Regex.IsMatch("^-?\d+(\.\d+)?$");

should test whether it is a decimal value. What it won't tell you is whether it is a valid decimal, as in, will the number fit in the range of a decimal.

Michael Bray
Neither will it detect if non-English separators are used.
Øyvind Skaar
You probably won't be able to cover all international variations of a numeric representation with a single regex, though :-(
marc_s
+13  A: 

I would consider exactly what you need to determine. "Is numeric" is vaguer than it sounds at first. Consider the following strings, and whether you'd want to consider them numeric:

  • "NaN"
  • "Nan"
  • "Infinity"
  • "0.000000000000000000000000000000000000000000000000001"
  • "1e5"
  • "1e500"
  • "1,000"
  • "+1"

Using Double.TryParse (with an en-GB culture - don't forget about cultural issues!) will give you True, False, True, True (despite it not being representable), True, False. True, True.

If you want to tell whether a later call to Double.TryParse would succeed, calling it here will be the most accurate solution. If you're using some other criteria, a regular expression may well be more appropriate. An example of the criteria you might use:

  • The can be a + or -, but only in the first character
  • There can be a single period at any character. You may want to avoid one at the end - should "1." be valid?
  • Other than the above, all characters must be digits

That would disallow all but the fourth and last examples above.

EDIT: I've now noticed the title of the question includes "integer". That pretty much reduces the specification checks to:

  • Do you want to allow leading zeroes (e.g. -00012)?
  • What is the range?
  • Do you only need decimals (instead of hex etc)?
  • Do you need to accept thousands separators?
  • What's your policy on leading/trailing whitespace?
Jon Skeet
+1  A: 

I just fired up Visual Studio Express (both 2005 and 2008). The Intellisense says that the return value of Double.TryParse() is a bool. The following worked for me under limited testing...

double res; // you must be under very resource-constrained 
            // conditions if you can't just declare a double
            // and forget about it

if (Double.TryParse(textBox1.Text, out res)) {
    label1.Text = "it's a number";
} else {
    label1.Text = "not a number";
}
John Ferguson
A: 

Hi,

try this isnumeric:

public static bool IsNumeric(object Expression)

{

  bool isNum;

  double retNum;

  isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any,System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );

  return isNum;

}

Ric Tokyo