Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can tell if my object's value is a float or int?

Tags:

c#

.net

asp.net

How can tell if my object's value is a float or int?

For example, I would like this to return me bool value.

like image 414
ALEXALEXIYEV Avatar asked Jun 11 '09 13:06

ALEXALEXIYEV


3 Answers

I'm assuming you mean something along the lines of...

if (value is int) {
  //...
}

if (value is float) {
  //...
}
like image 153
hugoware Avatar answered Sep 17 '22 23:09

hugoware


Are you getting the value in string form? If so there is no way to unambiguously tell which one it isbecause there are certain numbers that can be represented by both types (quite a few in fact). But it is possible to tell if it's one or the other.

public bool IsFloatOrInt(string value) {
  int intValue;
  float floatValue;
  return Int32.TryParse(value, out intValue) || float.TryParse(value, out floatValue);
}
like image 22
JaredPar Avatar answered Sep 20 '22 23:09

JaredPar


if (value.GetType() == typeof(int)) {
    // ...
}
like image 39
mmx Avatar answered Sep 19 '22 23:09

mmx