Best way to check if string is numeric

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    Best way to check if string is numeric

    I have a string field and a decimal fields and was wondering what the best
    way to check for numeric is?

    I have to do something like:

    If (myAmount is numeric)
    {
    total += myAmount;
    }

    Thanks,

    Tom


  • Kalpesh

    #2
    Re: Best way to check if string is numeric

    Use Decimal.Parse or TryParse.
    All the numeric datatype might have Parse & TryParse.

    Kalpesh

    On Dec 18, 11:02 am, "tshad" <t...@dslextrem e.comwrote:
    I have a string field and a decimal fields and was wondering what the best
    way to check for numeric is?
    >
    I have to do something like:
    >
    If (myAmount is numeric)
    {
    total += myAmount;
    >
    }
    >
    Thanks,
    >
    Tom

    Comment

    • GS

      #3
      Re: Best way to check if string is numeric

      don't know if the best but you cant ry something along the line of

      private static Rage _isNumber = new Rage(@"^\d+$");

      public static bool IsInteger(strin g theValue)
      {
      Match m = _isNumber.Match (theValue);
      return m.Success;
      } //IsInteger



      "tshad" <tfs@dslextreme .comwrote in message
      news:uGrJRuTQIH A.5160@TK2MSFTN GP05.phx.gbl...
      I have a string field and a decimal fields and was wondering what the best
      way to check for numeric is?
      >
      I have to do something like:
      >
      If (myAmount is numeric)
      {
      total += myAmount;
      }
      >
      Thanks,
      >
      Tom
      >
      >

      Comment

      • Michael Nemtsev [MVP]

        #4
        Re: Best way to check if string is numeric

        Hello GS,

        "If u have a promblem an gonna solve it with RegExp then now u have two problems"
        (c) dont remember who

        ---
        WBR,
        Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

        "The greatest danger for most of us is not that our aim is too high and we
        miss it, but that it is too low and we reach it" (c) Michelangelo


        Gdon't know if the best but you cant ry something along the line of
        Gprivate static Rage _isNumber = new Rage(@"^\d+$");
        Gpublic static bool IsInteger(strin g theValue)
        G{
        GMatch m = _isNumber.Match (theValue);
        Greturn m.Success;
        G} //IsInteger


        Comment

        • Jon

          #5
          Re: Best way to check if string is numeric

          If I remember correctly, a possible down-side with .Parse and .TryParse is that if you're trying to
          read a number that is not terminated in white space (eg 25k) it will produce an error since it
          regards the k in this case as invalid. This only matters of course if your number doesn't end in
          white space.


          "Kalpesh" <shahkalpesh@gm ail.comwrote in message
          news:f25d0a6e-f923-4485-843b-6aa0d83dabfb@i2 9g2000prf.googl egroups.com...
          Use Decimal.Parse or TryParse.
          All the numeric datatype might have Parse & TryParse.

          Kalpesh


          Comment

          • GS

            #6
            Re: Best way to check if string is numeric

            true enough if one doesn't use regexp to begin with.
            of course try parse will be better for some application

            "Michael Nemtsev [MVP]" <nemtsev@msn.co mwrote in message
            news:3d9fba1a20 e7e8ca0fbe373aa 890@msnews.micr osoft.com...
            Hello GS,
            >
            "If u have a promblem an gonna solve it with RegExp then now u have two
            problems"
            (c) dont remember who
            >
            ---
            WBR,
            Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour
            >
            "The greatest danger for most of us is not that our aim is too high and we
            miss it, but that it is too low and we reach it" (c) Michelangelo
            >
            >
            Gdon't know if the best but you cant ry something along the line of
            Gprivate static Rage _isNumber = new Rage(@"^\d+$");
            Gpublic static bool IsInteger(strin g theValue)
            G{
            GMatch m = _isNumber.Match (theValue);
            Greturn m.Success;
            G} //IsInteger
            >
            >

            Comment

            • tshad

              #7
              Re: Best way to check if string is numeric

              "Michael Nemtsev [MVP]" <nemtsev@msn.co mwrote in message
              news:3d9fba1a20 e7e8ca0fbe373aa 890@msnews.micr osoft.com...
              Hello GS,
              >
              "If u have a promblem an gonna solve it with RegExp then now u have two
              problems"
              Why is RegExp a problem?

              Thanks,

              tom
              (c) dont remember who
              >
              ---
              WBR, Michael Nemtsev [.NET/C# MVP] :: blog:

              "The greatest danger for most of us is not that our aim is too high and we
              miss it, but that it is too low and we reach it" (c) Michelangelo
              >
              Gdon't know if the best but you cant ry something along the line of
              Gprivate static Rage _isNumber = new Rage(@"^\d+$");
              Gpublic static bool IsInteger(strin g theValue)
              G{
              GMatch m = _isNumber.Match (theValue);
              Greturn m.Success;
              G} //IsInteger
              >
              >

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Best way to check if string is numeric

                On Dec 18, 4:15 pm, "tshad" <t...@dslextrem e.comwrote:
                "If u have a promblem an gonna solve it with RegExp then now u have two
                problems"
                >
                Why is RegExp a problem?
                When it's used in a "hammer to crack a nut" sense, it produces far
                less maintainable code.
                For genuine pattern matching, it's fine - but often there's a simpler
                alternative.

                It's very easy to get things wrong, regular expressions vary between
                different platforms (and indeed between different versions of .NET),
                you have to bear in mind escaping, etc.

                Jon

                Comment

                • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

                  #9
                  RE: Best way to check if string is numeric

                  in the Microsoft.Visua lBasic namespace, there is a very complete
                  implementation of IsNumeric. Of course, you may not need or even want such a
                  "feature rich" implementation, but you could take a look.
                  -- Peter
                  Site: http://www.eggheadcafe.com
                  UnBlog: http://petesbloggerama.blogspot.com
                  MetaFinder: http://www.blogmetafinder.com


                  "tshad" wrote:
                  I have a string field and a decimal fields and was wondering what the best
                  way to check for numeric is?
                  >
                  I have to do something like:
                  >
                  If (myAmount is numeric)
                  {
                  total += myAmount;
                  }
                  >
                  Thanks,
                  >
                  Tom
                  >
                  >
                  >

                  Comment

                  • Paul E Collins

                    #10
                    Re: Best way to check if string is numeric

                    "GS" <gsmsnews.micro soft.comGS@msne ws.Nomail.comwr ote:
                    [regular expression for matching numbers]
                    - Doesn't handle negative numbers.
                    - Even if it did, might not handle foreign variations (e.g. symbol for
                    negative that isn't "-").
                    - Will match *long* strings of digits that are bigger than an integer
                    can store.

                    Int32.TryParse is a lot safer.

                    Eq.


                    Comment

                    • Greg

                      #11
                      Re: Best way to check if string is numeric

                      On Dec 17, 10:02 pm, "tshad" <t...@dslextrem e.comwrote:
                      I have a string field and a decimal fields and was wondering what the best
                      way to check for numeric is?
                      >
                      I have to do something like:
                      >
                      If (myAmount is numeric)
                      {
                      total += myAmount;
                      >
                      }
                      >
                      Thanks,
                      >
                      Tom
                      Tom,

                      Another approach (depending on your parsing needs) is to take a look
                      at an extended TextBox that deals specifically with numeric data. The
                      source code is small for this control.

                      How to: Create a Numeric Text Box


                      HTH

                      Greg


                      Comment

                      • Kalpesh

                        #12
                        Re: Best way to check if string is numeric

                        tshad,

                        Isn't Parse or TryParse a simple thing to do in this case?
                        MVPs can suggest better ways, if this doesn't help

                        Kalpesh

                        Comment

                        • Ben Voigt [C++ MVP]

                          #13
                          Re: Best way to check if string is numeric


                          "Kalpesh" <shahkalpesh@gm ail.comwrote in message
                          news:75d2df7b-f384-4d22-a348-f5439971a017@e1 0g2000prf.googl egroups.com...
                          tshad,
                          >
                          Isn't Parse or TryParse a simple thing to do in this case?
                          MVPs can suggest better ways, if this doesn't help
                          Parse or TryParse is in fact mandatory, because even if the string is a
                          numeric sequence of digits, you still can't use it for math. You *have* to
                          parse it into a proper numeric variable first.
                          >
                          Kalpesh

                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Best way to check if string is numeric

                            Ben Voigt [C++ MVP] <rbv@nospam.nos pamwrote:
                            Isn't Parse or TryParse a simple thing to do in this case?
                            MVPs can suggest better ways, if this doesn't help
                            >
                            Parse or TryParse is in fact mandatory, because even if the string is a
                            numeric sequence of digits, you still can't use it for math. You *have* to
                            parse it into a proper numeric variable first.
                            While that's true, sometimes only validation is required. For instance,
                            it's reasonably common in web applications to require some client-side
                            validation that the user has typed in a number, but not to actually
                            have to *deal* with it as a number until it reaches the server.

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                            World class .NET training in the UK: http://iterativetraining.co.uk

                            Comment

                            • Ben Voigt [C++ MVP]

                              #15
                              Re: Best way to check if string is numeric


                              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                              news:MPG.21dc90 2ee31f83373e@ms news.microsoft. com...
                              Ben Voigt [C++ MVP] <rbv@nospam.nos pamwrote:
                              Isn't Parse or TryParse a simple thing to do in this case?
                              MVPs can suggest better ways, if this doesn't help
                              >>
                              >Parse or TryParse is in fact mandatory, because even if the string is a
                              >numeric sequence of digits, you still can't use it for math. You *have*
                              >to
                              >parse it into a proper numeric variable first.
                              >
                              While that's true, sometimes only validation is required. For instance,
                              it's reasonably common in web applications to require some client-side
                              validation that the user has typed in a number, but not to actually
                              have to *deal* with it as a number until it reaches the server.
                              The OP said

                              <quote>
                              I have to do something like:

                              If (myAmount is numeric)
                              {
                              total += myAmount;
                              }
                              </quote>
                              >
                              --
                              Jon Skeet - <skeet@pobox.co m>
                              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                              World class .NET training in the UK: http://iterativetraining.co.uk

                              Comment

                              Working...