2

I have value 0.075445054945055 for a variable corrected count and have the following function. Basically I need a function which would see if the value is a numeric and greater than 0. My current function only works for integers and not for values like 0.075 etc.

The field correctedCount comes from a file when its parsed.

  var correctedCount
  int num;
  bool isNumeric = int.TryParse(correctedCount, out num);

  if (isNumeric)
   {

    }
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
tam tam
  • 1,870
  • 2
  • 21
  • 46

2 Answers2

8

You can use decimal.TryParse or double.TryParse

I4V
  • 34,891
  • 6
  • 67
  • 79
6

You could treat it as a double like so:

double num;
if (double.TryParse(correctedCount, out num))
{
    // it's at least a number, now verify it's > 0
    return num > 0;
}
else
{
    return false;
}

Edit: this works because numbers without decimal parts (e.g., "4") are still valid doubles, as well as "0.075"