14

I need to check if a variable I have is of the data type double. This is what I tried:

try
{
    double price = Convert.ToDouble(txtPrice.Text);
}
catch (FormatException)
{
    MessageBox.Show("Product price is not a valid price", "Product price error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return false;
}

I thought this would work, but obviously, I failed to realize if txtPrice.Text had anything other than a number in it, the Convert class will just parse it out.

How can I realiably check if a variable is a double?

James Dawson
  • 5,309
  • 20
  • 72
  • 126
  • "variable" is the wrong word. You're trying to determine if text can be converted to a floating-point value. If you were determining the type of a variable, you would use variable.GetType() == typeof(double) – Brannon Mar 22 '12 at 14:07

10 Answers10

41

Use this:

double price;
bool isDouble = Double.TryParse(txtPrice.Text, out price);
if(isDouble) {
  // double here
}
ionden
  • 12,536
  • 1
  • 45
  • 37
12

How can I realiably check if a variable is a double?

You need to be clearer about what you're really trying to do here. I don't think you're asking what you think you're asking, and it's worth being aware of the differences in terminology.

If you've got a variable which is declared to be of type double, then it's definitely a double. If you've got a variable which is declared to be of type object, ValueType or one of the interfaces it supports, then you can use

if (value is double)

But it sounds like what you really want to know is whether a string value is parseable as a double. For that, you should use double.TryParse - but you also need to think about what culture you're interested in. For example, would you view "15,5" as a valid double? European users might, but US users probably wouldn't. Do you want to allow thousands separators?

I would strongly advise you to use the overload which takes an IFormatProvider and use the appropriate culture. Even if the culture you're interested is the default, it's worth being explicit about that.

You probably want:

double result;
// For suitable values of text, style and culture...
bool valid = double.TryParse(text, style, culture, out result);

Then use the valid variable to determine whether or not it was actually parsed correctly. If valid is true, then the value of result is the parsed value. If valid is false, result will be 0.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
7

Use the Double.TryParse method:

double price;
if (Double.TryParse(txtPrice.Text, out price))
{
    Console.WriteLine(price);
}
else
{
    Console.WriteLine("Not a double!");
}
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
2

Couldn't you just use:

double.Parse(txtPrice.Text);

?

With this you will a FormatException saying "Input string was not in a correct format." if the string value isn't a double, which looks to be roughly what you were doing manually anyway.

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
2

Why dont you try something like this -

  double doubleVar;
    if( typeof(doubleVar) == double ) {
        printf("doubleVar is of type double!");
    }

This can easily check if the variable is of a type double .

geeky_monster
  • 8,672
  • 18
  • 55
  • 86
2

You could also use .GetType() to return the type of the variable if you are unsure what is being returned if a method is being called to generate the number. See http://msdn.microsoft.com/en-us/library/58918ffs(v=vs.71).aspx for an example.

Robert H
  • 11,520
  • 18
  • 68
  • 110
1

You may use

  • double.ParseExact or
  • use Regex to check if it is valid.
Aliostad
  • 80,612
  • 21
  • 160
  • 208
1

Double.TryParse is what you want.

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
1

So if I get your question right, you mean you only want to allow numbers right? If that's true, then maybe this will help you.

string Str = textBox1.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
if (isNum)
MessageBox.Show(Num.ToString());
else
MessageBox.Show("Invalid number");
Yuki Kutsuya
  • 3,968
  • 11
  • 46
  • 63
1

You can use double.TryParse() it will return false if it couldn't create a double.

Stephen Gilboy
  • 5,572
  • 2
  • 30
  • 36