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);
}
c
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 .
Use this:
double price;
bool isDouble = Double.TryParse(txtPrice.Text, out price);
if(isDouble) {
// double here
}
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.
Use the Double.TryParse method:
double price;
if (Double.TryParse(txtPrice.Text, out price))
{
Console.WriteLine(price);
}
else
{
Console.WriteLine("Not a double!");
}