-6

I'm trying to check if an object is a number or not.

The IsNumeric function below works most of the time except when I pass a value of "NaN"

So I have this:

    private void button1_Click(object sender, EventArgs e)
    {
        object obj = "NaN";

        bool check = IsNumeric(obj);

        if (check)
        {
            MessageBox.Show(obj.ToString() + " is a number");
        }


    }

    public bool IsNumeric(object Expression)
    {
        bool isNum;
        double retNum;
        isNum = Double.TryParse(Convert.ToString(Expression), out retNum);
        return isNum;
    }

But the IsNumeric funciton returns "true" which is a lie.

I found the suggestion of the IsNumeric function here: How do I identify if a string is a number?

How can I check if an object is numeric or not?

Community
  • 1
  • 1
Pabinator
  • 1,601
  • 1
  • 21
  • 25
  • 1
    are you familiar with `TypeOf` of `GetType()` – MethodMan Feb 05 '15 at 22:14
  • 1
    Is Byte[] numeric or not ? – Marty Feb 05 '15 at 22:15
  • 1
    https://msdn.microsoft.com/en-us/library/system.object.gettype%28v=vs.110%29.aspx `Object.GetType()` it is a thing in C# – MethodMan Feb 05 '15 at 22:16
  • @MethodMan I guess he wants to check string values. – Nadia Chibrikova Feb 05 '15 at 22:17
  • if he wanted to check string value then why is there a `IsNumeric` wow @NadiaChibrikova can you elaborate – MethodMan Feb 05 '15 at 22:18
  • Do you expect "NaN" _not_ to be recognized as a number? You could easily exclude that using an `if()` statement. – CodeCaster Feb 05 '15 at 22:21
  • 3
    @Pabinator, I'm confused as to whether you have an object of unknown type that you want to check (as your description suggests), or if you have a string value that you want to check (as your code suggests). – Katie Kilian Feb 05 '15 at 22:21
  • @MethodMan well a string can contain a word or a number... – Nadia Chibrikova Feb 05 '15 at 22:22
  • `object obj = "NaN";` `var typeObj = obj.GetType();` – MethodMan Feb 05 '15 at 22:22
  • @Pabinator what are you actually trying to check? :) – Nadia Chibrikova Feb 05 '15 at 22:23
  • There are some great solutions to this problem here: http://stackoverflow.com/questions/1130698/checking-if-an-object-is-a-number-in-c-sharp – Icemanind Feb 05 '15 at 22:24
  • @NadiaChibrikova try my example and put "123" it will still be a type of System.String vs 123 will be of type System.Int32 – MethodMan Feb 05 '15 at 22:27
  • @MethodMan I know that, but I think he wants to consider "123" as numeric – Nadia Chibrikova Feb 05 '15 at 22:29
  • doesn't matter - the code speaks for it's self.. and if `IsNumeric Function is used properly it wouldn't be an issue either.. – MethodMan Feb 05 '15 at 22:30
  • other examples as well can be found here -http://stackoverflow.com/questions/1130698/checking-if-an-object-is-a-number-in-c-sharp – MethodMan Feb 05 '15 at 22:30
  • also why not use the var as a string instead of `object` and it will be much easier to accurately determine if the string is a string or integer I will post an even simpler method.. – MethodMan Feb 05 '15 at 22:49
  • 1
    Are any of these numbers: "seven", "pi", "XLII", "0xDeadBeef", "GDP of the UK in 1993", "sqrt(-1)", "i", "e", "6.02e23", "2^128", "1 megaton TNT in grams of mass defect", "answer to the ultimate question of life, the universe, and everything"? Perhaps there's a [WolframAlpha](http://www.wolframalpha.com/) API that would help. – HABO Feb 05 '15 at 22:50

2 Answers2

2

If you don't mind adding a reference to the VB library you can use:

Microsoft.VisualBasic.Information.IsNumeric(expression)
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

If your only trouble is with "NaN" then try this: isNum = Double.TryParse(Convert.ToString(Expression), out retNum) && !Double.IsNaN(retNum);

Btw "Infinity" and "-Infinity" also will be numeric.

Nadia Chibrikova
  • 4,916
  • 1
  • 15
  • 17