How to determine if my object value is float or int?

How to determine if my object value is float or int?

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

+6
source share
7 answers

Do you get the value in string form? If this is the case, there is no way to say unambiguously which one is there, because there are certain numbers that can be represented by both types (quite a lot actually). But we can say that this is one or the other.

public bool IsFloatOrInt(string value) { int intValue; float floatValue; return Int32.TryParse(value, out intValue) || float.TryParse(value, out floatValue); } 
+3
source share

I suppose you mean something like ...

 if (value is int) { //... } if (value is float) { //... } 
+14
source share
 if (value.GetType() == typeof(int)) { // ... } 
+4
source share

You may need Double.TryParse and Int.TryParse, although it is assumed that you are working with a string specified by the user.

+4
source share

If you mean object , that a (in the box) float / int - if(obj is float) , etc.

If you mean string , which could be ... int.TryParse(string, out int) / float.TryParse(string, out float)

+2
source share

The TryParse method for different types returns a boolean value. You can use it as follows:

 string value = "11"; float f; int i; if (int.TryParse(value, out i)) Console.WriteLine(value + " is an int"); else if (float.TryParse(value, out f)) Console.WriteLine(value + " is a float"); 
+2
source share

I think it’s useful here here we decide the value of the object - is it INT or Float

  public class Program { static void Main() { Console.WriteLine("Please enter Any Number"); object value = Console.ReadLine(); float f; int i; if (int.TryParse(Convert.ToString( value), out i)) Console.WriteLine(value + " is an int"); else if (float.TryParse(Convert.ToString(value), out f)) Console.WriteLine(value + " is a float"); Console.ReadLine(); } } 
+1
source share

All Articles