converting from string to float problem

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

    converting from string to float problem

    Hello,

    How I can convert value from string to double.

    When I do this in that way:

    String sParam="150.00" ;
    double dbValue= Convert.ToDoubl e(szParam);

    I get System.FormatEc xeption

    Thomas



  • Nicole Calinoiu

    #2
    Re: converting from string to float problem

    Thomas,

    You might find the either Double.Parse or Double.TryParse to be a bit more
    useful than Convert.ToDoubl e.

    HTH,
    Nicole


    "ool" <sorry@no.spa m> wrote in message
    news:bk4jch$doj $1@atlantis.new s.tpi.pl...[color=blue]
    > Hello,
    >
    > How I can convert value from string to double.
    >
    > When I do this in that way:
    >
    > String sParam="150.00" ;
    > double dbValue= Convert.ToDoubl e(szParam);
    >
    > I get System.FormatEc xeption
    >
    > Thomas
    >
    >
    >[/color]


    Comment

    • Daniel Pratt

      #3
      Re: converting from string to float problem

      Hi Thomas,

      "ool" <sorry@no.spa m> wrote in message
      news:bk4jch$doj $1@atlantis.new s.tpi.pl...[color=blue]
      > Hello,
      >
      > How I can convert value from string to double.
      >
      > When I do this in that way:
      >
      > String sParam="150.00" ;
      > double dbValue= Convert.ToDoubl e(szParam);
      >
      > I get System.FormatEc xeption[/color]

      A hunch.

      I note that your post seems to originate from Poland, yes? According to
      Windows, the Polish culture uses a comma to seperate whole part from decimal
      part. Assuming the number you are trying to parse follows the English (U.S.)
      format, you could create a CultureInfo object and pass it as the second
      (IFormatProvide r) parameter to the Convert.ToDoubl e method.

      System.Globaliz ation.CultureIn fo enUSCulture =
      new System.Globaliz ation.CultureIn fo("en-US");

      String sParam = "150.00";
      double dbValue = Convert.ToDoubl e(szParam, enUSCulture);

      Regards,
      Dan


      Comment

      • Jon Skeet

        #4
        Re: converting from string to float problem

        ool <sorry@no.spa m> wrote:[color=blue]
        > How I can convert value from string to double.
        >
        > When I do this in that way:
        >
        > String sParam="150.00" ;
        > double dbValue= Convert.ToDoubl e(szParam);[/color]

        I presume your actual code has the same variable name in both places.
        [color=blue]
        > I get System.FormatEc xeption[/color]

        My guess is that you're in a culture where the decimal point is a comma
        rather than a dot. Try

        double dbValue = Convert.ToDoubl e (szParam,
        CultureInfo.Inv ariantCulture);

        (with a using System.Globaliz ation; statement at the top of your code).

        --
        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

        Working...