C#, How to check an object to see if it is a numeric value?
-
C#, How to check an object to see if it is a numeric value? something like this? if (IsNumeric(intnumber)) { return intnumber } else { return 0 }
-
int iValue bool isInteger = int.tryparse(yourObject, out iValue);
Until you realize this message has nothing to say, its too late to stop reading
-
C#, How to check an object to see if it is a numeric value? something like this? if (IsNumeric(intnumber)) { return intnumber } else { return 0 }
The only place that IsNumeric exists, is for a char. The TryParse methods take a string, so if you have an object, you need this: string testVal = myObject.ToString(); double d; if (double.TryParse(testVal, out d)) { // I used double because 23234.545 is a number and int.TryParse won't accept it // I assume double.TryParse can parse a whole number. } The other way to do it is bool isNumeric = true; foreach(char c in myObject.ToString()) { if (!char.IsNumber(c)) { isNumeric = false; break; } } But this is more code, and again, it depends on if you care about decimals, writing a method to check decimals is even more messy ( you must accept one and only one decimal, and you need to respect culture, some cultures use , as a decimal point.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
C#, How to check an object to see if it is a numeric value? something like this? if (IsNumeric(intnumber)) { return intnumber } else { return 0 }
object obj = 10; string str =Convert.ToString(obj); Console.WriteLine(char.IsNumber(str, 0)); Console.Read(); Hi friend u can use this functionality and solve the issue.
Continue...
-
object obj = 10; string str =Convert.ToString(obj); Console.WriteLine(char.IsNumber(str, 0)); Console.Read(); Hi friend u can use this functionality and solve the issue.
Continue...
Wow - after two correct answers, you offer an incorrect one. Brilliant. This will work *sometimes*, but is far from foolproof.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
C#, How to check an object to see if it is a numeric value? something like this? if (IsNumeric(intnumber)) { return intnumber } else { return 0 }
Assume obj is some
object
. To check if that is a number, you could use:bool isNumber = Regex.IsMatch(obj.ToString(), @"^(\-|\+)?\d+[.]?\d*$");
SkyWalker
-
C#, How to check an object to see if it is a numeric value? something like this? if (IsNumeric(intnumber)) { return intnumber } else { return 0 }
Assuming you had something like:
int number = 5;
...
object obj = number;or
object obj = 5;
Then you could just use:
if(obj.GetType() == typeof(Int32) {
//We have an int
}or even simpler still:
if(obj is Int32)
//Omg, an intYou may also want to check for Int16 and Int64 (or you can check for short, int and long, oh yeah and there's also float and double to check for as well. And the unsigned versions of the types)
My current favourite word is: Nipple!
-SK Genius
-
Assume obj is some
object
. To check if that is a number, you could use:bool isNumber = Regex.IsMatch(obj.ToString(), @"^(\-|\+)?\d+[.]?\d*$");
SkyWalker
That's just overkill.
-
That's just overkill.
It depends on what you are actually doing. Why would someone put that check inside a constant looping?
SkyWalker