converting strings to decimal

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

    #1

    converting strings to decimal

    I'm writing a small component that needs to be as fast as
    possible. The component needs to convert a string to
    decimal during the course of it's processing. However, I
    need to test the string first to make sure it is
    numeric. Using the is keyword doesn't work (strings
    cannot be cast as decimal so false is always returned)
    and catching an exception from Convert.ToDecim al or
    decimal.Parse is too slow.

    Does anyone know of any methods of testing strings to see
    if they are numerical without converting the string?

    Paul K
  • Jon Skeet [C# MVP]

    #2
    Re: converting strings to decimal

    Paul K <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
    > I'm writing a small component that needs to be as fast as
    > possible. The component needs to convert a string to
    > decimal during the course of it's processing. However, I
    > need to test the string first to make sure it is
    > numeric. Using the is keyword doesn't work (strings
    > cannot be cast as decimal so false is always returned)
    > and catching an exception from Convert.ToDecim al or
    > decimal.Parse is too slow.
    >
    > Does anyone know of any methods of testing strings to see
    > if they are numerical without converting the string?[/color]

    Well, you can do a fairly quick check to make sure that the first
    character is '+', '-', '.' (or other characters if you're parsing in an
    international way) or a digit - and that all following characters are
    either digits or '.' (or other separation characters, as before).

    You could do it with a regular expression if you really wanted to, but
    that would be slower than a hand-crafted routine.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Frank Oquendo

      #3
      Re: converting strings to decimal

      Paul K wrote:
      [color=blue]
      > Does anyone know of any methods of testing strings to see
      > if they are numerical without converting the string?[/color]

      double.TryParse ()

      --
      There are 10 kinds of people. Those who understand binary and those who
      don't.


      (Pull the pin to reply)


      Comment

      • Madhu [MVP]

        #4
        converting strings to decimal

        Hi,

        The best and fastest way to test the string is a decimal
        is using RegEx.

        The sample code snippet is given below,

        string intValue = textBox1.Text;

        Regex regex = new Regex (@"(^-?\d*\.?
        \d*\d+\d*$)",Re gexOptions.None );

        Match match = regex.Match(int Value);

        if (!match.Success )
        {
        MessageBox.Show ("Invalid Decimal...");
        }
        else
        MessageBox.Show ("valid Decimal...");

        Hope this helps...

        Regards,
        Madhu

        MVP | MCSD.NET
        [color=blue]
        >-----Original Message-----
        >I'm writing a small component that needs to be as fast[/color]
        as[color=blue]
        >possible. The component needs to convert a string to
        >decimal during the course of it's processing. However,[/color]
        I[color=blue]
        >need to test the string first to make sure it is
        >numeric. Using the is keyword doesn't work (strings
        >cannot be cast as decimal so false is always returned)
        >and catching an exception from Convert.ToDecim al or
        >decimal.Pars e is too slow.
        >
        >Does anyone know of any methods of testing strings to[/color]
        see[color=blue]
        >if they are numerical without converting the string?
        >
        >Paul K
        >.
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: converting strings to decimal

          Madhu [MVP] <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
          > The best and fastest way to test the string is a decimal
          > is using RegEx.[/color]

          No it's not.

          It's significantly faster to use a hand-crafted routine.

          When parsing ints a while ago I got the following results (times) for
          parsing some larger number (some valid, some invalid) of strings:

          Straight exceptions: 00:01:15.798993 6
          Hard coded routine: 00:00:00.701008 0
          Double.TryParse : 00:00:40.838723 2
          Regular expressions: 00:00:43.041891 2
          IsNumeric from VisualBasic.Inf ormation: 00:01:06.906206 4

          That was for a very simple regular expression:
          ^[+-]?\d+$

          While obviously the results would be *slightly* different for parsing
          decimals, I doubt that they'd change enough to make regular expressions
          faster than the hand-coded routine.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Paul K

            #6
            converting strings to decimal

            Madhu,

            I tried both the Regex class and using my own test
            function -- both are the same speed with the current
            amount of data that I have. I'll have to generate more
            data to get a reliable reading.

            Regardless of which is technically better, thank you for
            your info. The component is now processing at an
            acceptable speed.

            Thanks!

            Paul K[color=blue]
            >-----Original Message-----
            >Hi,
            >
            >The best and fastest way to test the string is a decimal
            >is using RegEx.
            >
            >The sample code snippet is given below,
            >
            >string intValue = textBox1.Text;
            >
            >Regex regex = new Regex (@"(^-?\d*\.?
            >\d*\d+\d*$)",R egexOptions.Non e);
            >
            >Match match = regex.Match(int Value);
            >
            >if (!match.Success )
            >{
            > MessageBox.Show ("Invalid Decimal...");
            >}
            >else
            > MessageBox.Show ("valid Decimal...");
            >
            >Hope this helps...
            >
            >Regards,
            >Madhu
            >
            >MVP | MCSD.NET
            >[color=green]
            >>-----Original Message-----
            >>I'm writing a small component that needs to be as fast[/color]
            >as[color=green]
            >>possible. The component needs to convert a string to
            >>decimal during the course of it's processing. However,[/color]
            >I[color=green]
            >>need to test the string first to make sure it is
            >>numeric. Using the is keyword doesn't work (strings
            >>cannot be cast as decimal so false is always returned)
            >>and catching an exception from Convert.ToDecim al or
            >>decimal.Par se is too slow.
            >>
            >>Does anyone know of any methods of testing strings to[/color]
            >see[color=green]
            >>if they are numerical without converting the string?
            >>
            >>Paul K
            >>.
            >>[/color]
            >.
            >[/color]

            Comment

            • Paul K

              #7
              Re: converting strings to decimal

              Jon,

              I've tried my own test function as well as the Regex
              class -- both are the same speed with the current amount
              of data I have. I'll have to generate more data to get a
              reliable reading.

              Regardless of which one is technically faster, thank you
              for your info. The component is now processing at an
              acceptable speed.

              Thanks!

              Paul K[color=blue]
              >-----Original Message-----
              >Paul K <anonymous@disc ussions.microso ft.com> wrote:[color=green]
              >> I'm writing a small component that needs to be as fast[/color][/color]
              as[color=blue][color=green]
              >> possible. The component needs to convert a string to
              >> decimal during the course of it's processing.[/color][/color]
              However, I[color=blue][color=green]
              >> need to test the string first to make sure it is
              >> numeric. Using the is keyword doesn't work (strings
              >> cannot be cast as decimal so false is always returned)
              >> and catching an exception from Convert.ToDecim al or
              >> decimal.Parse is too slow.
              >>
              >> Does anyone know of any methods of testing strings to[/color][/color]
              see[color=blue][color=green]
              >> if they are numerical without converting the string?[/color]
              >
              >Well, you can do a fairly quick check to make sure that[/color]
              the first[color=blue]
              >character is '+', '-', '.' (or other characters if[/color]
              you're parsing in an[color=blue]
              >internationa l way) or a digit - and that all following[/color]
              characters are[color=blue]
              >either digits or '.' (or other separation characters, as[/color]
              before).[color=blue]
              >
              >You could do it with a regular expression if you really[/color]
              wanted to, but[color=blue]
              >that would be slower than a hand-crafted routine.
              >
              >--
              >Jon Skeet - <skeet@pobox.co m>
              >http://www.pobox.com/~skeet
              >If replying to the group, please do not mail me too
              >.
              >[/color]

              Comment

              • Paul K

                #8
                Re: converting strings to decimal

                Frank,

                I tried TryParse() -- it's a little too slow for my
                component (with about 300 rows of data, there's about a
                half-second performance hit between TryParse and using
                either regular expressions or my own function).

                Thanks anyway!

                Paul K[color=blue]
                >-----Original Message-----
                >Paul K wrote:
                >[color=green]
                >> Does anyone know of any methods of testing strings to[/color][/color]
                see[color=blue][color=green]
                >> if they are numerical without converting the string?[/color]
                >
                >double.TryPars e()
                >
                >--
                >There are 10 kinds of people. Those who understand[/color]
                binary and those who[color=blue]
                >don't.
                >
                >http://code.acadx.com
                >(Pull the pin to reply)
                >
                >
                >.
                >[/color]

                Comment

                Working...