Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. 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?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dougins
    wrote on last edited by
    #1

    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 }

    D C J Mircea PuiuM A 5 Replies Last reply
    0
    • D dougins

      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 }

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      int iValue bool isInteger = int.tryparse(yourObject, out iValue);

      Until you realize this message has nothing to say, its too late to stop reading

      D 1 Reply Last reply
      0
      • D dan sh

        int iValue bool isInteger = int.tryparse(yourObject, out iValue);

        Until you realize this message has nothing to say, its too late to stop reading

        D Offline
        D Offline
        dougins
        wrote on last edited by
        #3

        Can we put it an if statement? If (isNumeric(myObject) { return myObject }

        D 1 Reply Last reply
        0
        • D dougins

          Can we put it an if statement? If (isNumeric(myObject) { return myObject }

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          dougins wrote:

          If (isNumeric(myObject)

          Where did isNumeric came from? If you are talking about Microsoft.VisualBasic.Information.IsNumeric(object) then cant use it.

          Until you realize this message has nothing to say, its too late to stop reading

          1 Reply Last reply
          0
          • D dougins

            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 }

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            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 )

            1 Reply Last reply
            0
            • D dougins

              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 }

              J Offline
              J Offline
              Jeeva Jose
              wrote on last edited by
              #6

              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...

              C 1 Reply Last reply
              0
              • J Jeeva Jose

                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...

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                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 )

                1 Reply Last reply
                0
                • D dougins

                  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 }

                  Mircea PuiuM Offline
                  Mircea PuiuM Offline
                  Mircea Puiu
                  wrote on last edited by
                  #8

                  Assume obj is some object. To check if that is a number, you could use: bool isNumber = Regex.IsMatch(obj.ToString(), @"^(\-|\+)?\d+[.]?\d*$");

                  SkyWalker

                  B 1 Reply Last reply
                  0
                  • D dougins

                    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 }

                    A Offline
                    A Offline
                    Anthony Mushrow
                    wrote on last edited by
                    #9

                    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 int

                    You 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

                    1 Reply Last reply
                    0
                    • Mircea PuiuM Mircea Puiu

                      Assume obj is some object. To check if that is a number, you could use: bool isNumber = Regex.IsMatch(obj.ToString(), @"^(\-|\+)?\d+[.]?\d*$");

                      SkyWalker

                      B Offline
                      B Offline
                      blackjack2150
                      wrote on last edited by
                      #10

                      That's just overkill.

                      Mircea PuiuM 1 Reply Last reply
                      0
                      • B blackjack2150

                        That's just overkill.

                        Mircea PuiuM Offline
                        Mircea PuiuM Offline
                        Mircea Puiu
                        wrote on last edited by
                        #11

                        It depends on what you are actually doing. Why would someone put that check inside a constant looping?

                        SkyWalker

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups