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. How can I check if a string is numeric before actually converting it to a number? [modified]

How can I check if a string is numeric before actually converting it to a number? [modified]

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 4 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.
  • C Offline
    C Offline
    cateyes99
    wrote on last edited by
    #1

    In C#, the Parse method of int32, Double, single will raise an FormatExcepton if the input string is invalid (non-numeric) during conversion. Is there an easy way to check it before just calling the Parse method and then receiving such an exception? I don't wanna write a wrapper and catch such an exception which would be stupid for this kind of conversion. Thanks. -- modified at 23:18 Thursday 29th March, 2007

    C 1 Reply Last reply
    0
    • C cateyes99

      In C#, the Parse method of int32, Double, single will raise an FormatExcepton if the input string is invalid (non-numeric) during conversion. Is there an easy way to check it before just calling the Parse method and then receiving such an exception? I don't wanna write a wrapper and catch such an exception which would be stupid for this kind of conversion. Thanks. -- modified at 23:18 Thursday 29th March, 2007

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

      int.TryParse double.TryParse

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      C 1 Reply Last reply
      0
      • C Christian Graus

        int.TryParse double.TryParse

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        C Offline
        C Offline
        cateyes99
        wrote on last edited by
        #3

        Thank you. But those TryParse methods are new in .Net 2; actually I need similar stuff in .Net 1.1 .

        M 1 Reply Last reply
        0
        • C cateyes99

          Thank you. But those TryParse methods are new in .Net 2; actually I need similar stuff in .Net 1.1 .

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          No, they aren't. double.TryParse() definitely is part of 1.1!

          Regards, mav -- Black holes are the places where God divided by 0...

          C 1 Reply Last reply
          0
          • M mav northwind

            No, they aren't. double.TryParse() definitely is part of 1.1!

            Regards, mav -- Black holes are the places where God divided by 0...

            C Offline
            C Offline
            cateyes99
            wrote on last edited by
            #5

            Just checked: double.TryParse() is available in 1.1, but there is no such version for int32 and Single. Thanks,

            M 1 Reply Last reply
            0
            • C cateyes99

              Just checked: double.TryParse() is available in 1.1, but there is no such version for int32 and Single. Thanks,

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello, You could use double.TryParse with "NumberStyles.Integer" and then Convert the doubleValue to Integer.

              double d;
              int yourInt;
              if(double.TryParse(YoureString,System.Globalization.NumberStyles.Integer, CultureInfo.CurrentCulture, out d))
              {
              yourInt = Convert.ToInt32(d);
              }

              All the best, Martin

              C 1 Reply Last reply
              0
              • M Martin 0

                Hello, You could use double.TryParse with "NumberStyles.Integer" and then Convert the doubleValue to Integer.

                double d;
                int yourInt;
                if(double.TryParse(YoureString,System.Globalization.NumberStyles.Integer, CultureInfo.CurrentCulture, out d))
                {
                yourInt = Convert.ToInt32(d);
                }

                All the best, Martin

                C Offline
                C Offline
                cateyes99
                wrote on last edited by
                #7

                Thanks very much!

                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