convert string to a float

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mtaylor314
    New Member
    • Jun 2007
    • 4

    convert string to a float

    I am needing some help converting a string to a float value. The string could be in any of the following formats: x x.y x/y. Obviously, I am having issues w/ the fraction string. The other formats are very easy. I have searched other places online but I cannot find anything on the fraction format. Any suggestions for this fairly n00b would be appreciated.
  • TRScheel
    Recognized Expert Contributor
    • Apr 2007
    • 638

    #2
    Originally posted by mtaylor314
    I am needing some help converting a string to a float value. The string could be in any of the following formats: x x.y x/y. Obviously, I am having issues w/ the fraction string. The other formats are very easy. I have searched other places online but I cannot find anything on the fraction format. Any suggestions for this fairly n00b would be appreciated.

    Well if you are going to take those three formats, you should first ensure that your string is of a proper format. Once there, you need to do two different things. If its one of the first two formats, just parse it. If its the last, you need to split it by the / and then check each of the two numbers, which I assume, each can be a form of the first two (hence, you can have x.y/z or even a.b/x.y).

    Here is some sample code:

    [code=cpp]
    [STAThread]
    static void Main(string[] args)
    {
    Console.WriteLi ne(ParseStringF orDoubleExpress ion("5.3"));
    Console.WriteLi ne(ParseStringF orDoubleExpress ion("7"));
    Console.WriteLi ne(ParseStringF orDoubleExpress ion("4/5.4"));
    try
    {
    Console.WriteLi ne(ParseStringF orDoubleExpress ion("4/5/3"));
    }
    catch
    {
    Console.WriteLi ne("Exception handled");
    }
    Console.ReadKey (true);
    }

    private static double ParseStringForD oubleExpression (string value)
    {
    double result = 0.0;
    if (string.IsNullO rEmpty(value))
    throw new NullReferenceEx ception("Parame ter 'value' cannot be null or empty");

    if (value.Contains ("/"))
    {
    result = ParseForDivisio n(value);
    }
    else
    {
    result = GetDoubleValue( value);
    }

    return result;
    }

    private static double ParseForDivisio n(string value)
    {
    if (value.Contains ("/"))
    {
    if (value.Split('/').Length != 2)
    throw new Exception("Para meter 'value' is of incorrect format. Acceptable formats are: X or X.Y or X/Y");
    }
    else
    {
    throw new Exception("Para meter 'value' is of incorrect format. Acceptable formats are: X or X.Y or X/Y");
    }

    double firstResult = GetDoubleValue( value.Split('/')[0]);
    double secondResult = GetDoubleValue( value.Split('/')[1]);

    return firstResult / secondResult;
    }

    private static double GetDoubleValue( string value)
    {
    long placeHolder;
    double result;
    if (CheckForLong(v alue, out placeHolder))
    {
    result = placeHolder;
    }
    else if (CheckForDouble (value, out result))
    { }
    else
    {
    throw new Exception("Para meter 'value' is of incorrect format. Acceptable formats are: X or X.Y or X/Y");
    }

    return result;
    }

    private static bool CheckForDouble( string value, out double result)
    {
    if (double.TryPars e(value, out result))
    {
    if (value == result.ToString ())
    {
    return true;
    }
    }

    return false;
    }

    private static bool CheckForLong(st ring value, out long result)
    {
    if (long.TryParse( value, out result))
    {
    if (value == result.ToString ())
    {
    return true;
    }
    }

    return false;
    }
    [/code]

    Comment

    • mtaylor314
      New Member
      • Jun 2007
      • 4

      #3
      I should have clarified previously, but I am developing in C# using Visual Studio 2003. There isn't oString.contain s method, which is the tricky part. I already tried using that. I do appreciate the help though. If there are any other suggestions I will gladly take them.

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        Originally posted by mtaylor314
        I should have clarified previously, but I am developing in C# using Visual Studio 2003. There isn't oString.contain s method, which is the tricky part. I already tried using that. I do appreciate the help though. If there are any other suggestions I will gladly take them.
        Use IndexOf instead then. If it returns -1, it doesnt exist. If it returns a value, split to that value, and check from that value on. If it isnt -1, '/' appears more then once.

        Example:

        [code=cpp]
        string myString = "whatever";
        if(myString.Ind exOf('/') != -1)
        {
        if(myString.Sub string(myString .IndexOf('/'), myString.Length - myString.IndexO f('/') - 1) != -1)
        { // too many '/' }
        else
        { // right amount
        }
        }
        [/code]

        Comment

        Working...