locked
Double tryprase plz help RRS feed

  • Question

  • Hello

              I have a function below which take string and return true if string in valid double No else false.

    If I send "-0.7"  it return true ...

    BUT if I send "-.7" then it return false.... It should return me true

    public static bool IsValidDoubleNumberWithNegativeValue(string val)
            {
                bool isValidNumber = true;
                Double reusltMinValue;
                Double.TryParse(val, out reusltMinValue);

                if (!val.Equals(reusltMinValue.ToString()))
                {
                    isValidNumber = false;
                }

                return isValidNumber;
            }

    Tuesday, May 8, 2012 7:25 AM

Answers

  • Heslacher is right, but in addition you need to tell to TryParse method that the number allows the leading symbol:

    public static bool IsValidDoubleNumberWithNegativeValue(string val)
    {
        double reusltMinValue;
        var isValidNumber = Double.TryParse(val, System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowLeadingSign, 
                            new System.Globalization.NumberFormatInfo { NumberDecimalSeparator = "." }, out reusltMinValue);
        return isValidNumber;
    }

    Note that, in this example, I have explicitly set the decimal separator, using NumberFormatInfoClass, because on my Italian system it is the comma. According to your locale, you don't need this, but I prefer to set it in any case.


    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Tuesday, May 8, 2012 7:37 AM
  • Depending on your system settings, you need to specify the decimal separator, e.g.

                Double reusltMinValue;
                CultureInfo formatProvider = new CultureInfo("en-US");
                // When using another Culture:
                // formatProvider.NumberFormat.NumberDecimalSeparator = ".";            
                bool isValidNumber = Double.TryParse(val, NumberStyles.Any, formatProvider, out reusltMinValue);
    

    Tuesday, May 8, 2012 7:44 AM
  • Tuesday, May 8, 2012 9:04 AM

All replies

  • why don`t you use the returnvalue of the TryParse() method itself ?

    This can be just a rounding issue.

    isValidNumber = Double.TryParse(val, reusltMinValue);


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/


    • Edited by Heslacher Tuesday, May 8, 2012 7:31 AM
    Tuesday, May 8, 2012 7:30 AM
  • Heslacher is right, but in addition you need to tell to TryParse method that the number allows the leading symbol:

    public static bool IsValidDoubleNumberWithNegativeValue(string val)
    {
        double reusltMinValue;
        var isValidNumber = Double.TryParse(val, System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowLeadingSign, 
                            new System.Globalization.NumberFormatInfo { NumberDecimalSeparator = "." }, out reusltMinValue);
        return isValidNumber;
    }

    Note that, in this example, I have explicitly set the decimal separator, using NumberFormatInfoClass, because on my Italian system it is the comma. According to your locale, you don't need this, but I prefer to set it in any case.


    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Tuesday, May 8, 2012 7:37 AM
  • Depending on your system settings, you need to specify the decimal separator, e.g.

                Double reusltMinValue;
                CultureInfo formatProvider = new CultureInfo("en-US");
                // When using another Culture:
                // formatProvider.NumberFormat.NumberDecimalSeparator = ".";            
                bool isValidNumber = Double.TryParse(val, NumberStyles.Any, formatProvider, out reusltMinValue);
    

    Tuesday, May 8, 2012 7:44 AM
  • Hello

              thanks for reply.I also want a function which tell me weather the value is "long" or not  no decmial value 

    Tuesday, May 8, 2012 8:53 AM
  • Tuesday, May 8, 2012 9:04 AM