Convert.toDouble input string was not in correct format

Last post 07-04-2009 3:36 PM by veddzik. 7 replies.

Sort Posts:

  • Convert.toDouble input string was not in correct format

    07-03-2009, 12:03 PM
    • Member
      2 point Member
    • veddzik
    • Member since 02-28-2008, 2:53 AM
    • Posts 23

    Hello,

    I can't figure out why it' possible to convert my string. Error message is: input string was not in correct format

    This is my code, which make an error:

    Dim dbl as Double
    Dim str as String = "£22.50")
    str = str.Replace("£", "")
    str = str.Replace(".", ",")
    dbl = Convert.ToDouble(str)

    But if I try this, it's work:

    dbl = Convert.ToDouble("22,50")

    What's going on ?

    jeremy is still alive
  • Re: Convert.toDouble input string was not in correct format

    07-03-2009, 3:07 PM

    {

    string s = "22.45";
            double p = Convert.ToDouble(s);
            Response.Write(p.ToString());

    }


    This worked perfectly fine.

    Saurabh Nijhawan(B.Tech. CSE,GGSIPU,New Delhi)
    Application Architect, Eminent Solutions, New Delhi.
    Freelancer | Teacher
    Remember to click "Mark as Answer" on the post, if it helped you.
    ASP.NET Weblog
    http://www.saurabhnijhawan.com
    Learning Made Easy


  • Re: Convert.toDouble input string was not in correct format

    07-03-2009, 3:23 PM

     Hi,

    Dim dbl As Double
            Dim str As String = "£22.50"
            Str = Str.Replace("£", "")
            Str = Str.Replace(".", ",")
            dbl = Convert.ToDouble(str)
            TextBox1.Text = dbl;

    This is working with me

     

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: Convert.toDouble input string was not in correct format

    07-03-2009, 7:49 PM
    • Participant
      1,547 point Participant
    • ctheriault
    • Member since 04-10-2009, 4:33 PM
    • Posts 276

     It also depends of the current culture of the thread executing Convert.ToDouble()

    For instance Convert.ToDouble("22,50") works fine if CurrentCulture is French because French use a coma as decimal separator.

    You don't necessare need to change the default of the thread's culture; you can define the convesion provider. You don't necessary need to remove the currency symbol either if you provide the proper conversion params. For instance in the snippet below I also assumed you would need to have a space as group separator instead of the american style's coma:

     

    System.Globalization.NumberFormatInfo nfi = new
    System.Globalization.NumberFormatInfo();
    nfi.CurrencySymbol = "£";
    nfi.CurrencyDecimalSeparator = ",";
    nfi.NumberGroupSeparator = " ";
    nfi.NumberDecimalDigits = 2;
    decimal d = Decimal.Parse("£78 022.50", nfi);


    Bye

     

     

    ----
    Don't forget to mark this posting as an "Answer" if it is helpful to you
  • Re: Convert.toDouble input string was not in correct format

    07-03-2009, 8:40 PM
    Answer
    • Star
      12,577 point Star
    • malcolms
    • Member since 06-12-2008, 4:38 AM
    • Melbourne, Australia
    • Posts 2,062

    A good idea also is to use TryParse.  That will catch any errors:

    double result = 0;
    if(double.TryParse(yourstring, out result))
    {
          // valid double
    }


    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Convert.toDouble input string was not in correct format

    07-04-2009, 10:38 AM
    • Star
      11,849 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,328

    I tried the code

    string ax = "$22.50";
            ax = ax.Replace("$", "");
            ax = ax.Replace(".", ",");
            Response.Write("ax is " + ax);

            double dx = Convert.ToDouble(ax);
            Response.Write("dx is: " +dx.ToString());

    and it works fine.

  • Re: Convert.toDouble input string was not in correct format

    07-04-2009, 10:44 AM
    • Member
      80 point Member
    • hminaya
    • Member since 10-17-2008, 3:04 AM
    • Dominican Republic
    • Posts 31

     What culture are u using?

    Hector Minaya
    Microsoft Visual Basic MVP | Speaker INETA - Latam | MCSD | MCT | MCTS : SQL Server
    blogs: Hector Minaya | DotNetNuke SEO | Blogger SEO | facebook junkie | SEO Tools
  • Re: Convert.toDouble input string was not in correct format

    07-04-2009, 3:36 PM
    • Member
      2 point Member
    • veddzik
    • Member since 02-28-2008, 2:53 AM
    • Posts 23

    Thank you guys for all your answers. Method TryParse resolved my problem.

    jeremy is still alive
Page 1 of 1 (8 items)