atoi and atof Equivalents in .NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poe
    New Member
    • Jun 2007
    • 32

    #1

    atoi and atof Equivalents in .NET

    Hello,

    Are there any equivalents to the c/c++ functions atoi and atof in the .NET framework? I'd like to be able to parse a string like these two functions, but I haven't been able to find anything that works near as good. Most of the .NET functions I'm aware of are not very robust.

    Thank.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Code:
    string s = "12345";
    int i = Convert.ToInt32(s);
    s = "123.45";
    double d = Convert.ToDouble(s);
    This can convert from many other data types as well. As for float, I think you'd have to cast from a double.

    There is also:
    Code:
    int i = Integer.Parse("12345");
    double d = Double.Parse("123.45");
    And there is also the TryParse methods.

    Comment

    • poe
      New Member
      • Jun 2007
      • 32

      #3
      Originally posted by insertAlias
      Code:
      string s = "12345";
      int i = Convert.ToInt32(s);
      s = "123.45";
      double d = Convert.ToDouble(s);
      This can convert from many other data types as well. As for float, I think you'd have to cast from a double.

      There is also:
      Code:
      int i = Integer.Parse("12345");
      double d = Double.Parse("123.45");
      And there is also the TryParse methods.
      I've tried all the TryParse, Parse and Convert methods, but like I mentioned above, they aren't very robust. For example, lets say you have a string like "1.2 seconds" or "16.5 MB" any of the above functions will throw an exception unlike their c/c++ counterparts.

      Comment

      • cloud255
        Recognized Expert Contributor
        • Jun 2008
        • 427

        #4
        Originally posted by poe
        I've tried all the TryParse, Parse and Convert methods, but like I mentioned above, they aren't very robust. For example, lets say you have a string like "1.2 seconds" or "16.5 MB" any of the above functions will throw an exception unlike their c/c++ counterparts.

        for this you need to look at each individual character in the string or edit the UI so that the user will not enre "1.2 seconds" but rather enter 1.2 and then select the time frame from a control like a combo box.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          For this I would agree with cloud. It's not the lack of robustness in the Parse() functions. (The overloads have some pretty good power behind them) but more a fault in the UI for allowing units and other text into the numeric string.
          There are a number of regular expression and/or character match functions (.IsDigit for example) that could be used to step through the string and pull out only the usable sections.

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by Plater
            For this I would agree with cloud. It's not the lack of robustness in the Parse() functions. (The overloads have some pretty good power behind them) but more a fault in the UI for allowing units and other text into the numeric string.
            There are a number of regular expression and/or character match functions (.IsDigit for example) that could be used to step through the string and pull out only the usable sections.
            Agreed, if you're looking for the first characters that are assumed to be numeric, then you should be seeking something like

            Code:
            Dim val As Double = Regex.Match(InputString, "^\d+(\.\d+)?|(\.\d+)(?=[^\D\.])").Value;
            That will strip out any leading valid number. For instance 123.45.678.9 it will only pick out 123.45, likewise 123.45Hello World it will only pick out 123.45 and "123.45 Hello World" it will still only pick out 123.45.

            I have to agree with one of the previous posters - this sounds like a bad design idea. A numeric input should only allow numeric input. If someone types in something else, there should be an exception raised and it should be caught and handled properly rather than compensating for bad input.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              I'll have to remember that regex. Nice one.

              Comment

              Working...