Double conversion problem

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

    #1

    Double conversion problem

    Hi there,

    I'm having a weird situation when converting strings to double.
    Normally, the following code would work right?

    double MyDouble = double.Parse("2 0.50"); // Error at runtime

    Well, it gives me an error at run time, telling me that the format of
    the input string is incorrect. Then, if i try with a comma instead of
    a dot, it works fine.

    double MyDouble = double.Parse("2 0,50"); // Works fine

    Isn't it supposed to be a dot that works? I think it could be related
    to "Regional and Language Options" in WinXP. I'm using french canadian
    regional options if it can gives you a hint. And if it is really
    related to regional and language options, what should i change
    exactly?

    If anyone has an idea how to make it works with a dot that would be
    great. I want understand why it doesn't works. Thanks!
  • mattihara

    #2
    Re: Double conversion problem

    I'm sure it is your regional settings.

    You will notice that in the Regional Options Tab (in the Regional and
    Language Options) the samples will show you how the number is to be
    formatted. For French Canadian, a number is this:

    "123 456 789,00"

    which would explain your error. Change the format to English and you won't
    have the problem.

    If you must have the french Canadian, then you have to use that number
    format, where a comma is the decimal separator. You could also customize
    and change the decimal to a period.

    Hope this helps.


    "Matt" <metal@rocks.co m> wrote in message
    news:kitdivcspd dgmefj3fiundh0k i9dv7cf1d@4ax.c om...[color=blue]
    > Hi there,
    >
    > I'm having a weird situation when converting strings to double.
    > Normally, the following code would work right?
    >
    > double MyDouble = double.Parse("2 0.50"); // Error at runtime
    >
    > Well, it gives me an error at run time, telling me that the format of
    > the input string is incorrect. Then, if i try with a comma instead of
    > a dot, it works fine.
    >
    > double MyDouble = double.Parse("2 0,50"); // Works fine
    >
    > Isn't it supposed to be a dot that works? I think it could be related
    > to "Regional and Language Options" in WinXP. I'm using french canadian
    > regional options if it can gives you a hint. And if it is really
    > related to regional and language options, what should i change
    > exactly?
    >
    > If anyone has an idea how to make it works with a dot that would be
    > great. I want understand why it doesn't works. Thanks![/color]


    Comment

    • Jack Hanebach

      #3
      Re: Double conversion problem

      "Matt" <metal@rocks.co m> wrote in message
      news:kitdivcspd dgmefj3fiundh0k i9dv7cf1d@4ax.c om...
      [color=blue]
      > double MyDouble = double.Parse("2 0.50"); // Error at runtime
      >
      > Well, it gives me an error at run time, telling me that the format of
      > the input string is incorrect. Then, if i try with a comma instead of
      > a dot, it works fine.
      >
      > double MyDouble = double.Parse("2 0,50"); // Works fine
      >
      > Isn't it supposed to be a dot that works? I think it could be related
      > to "Regional and Language Options" in WinXP. I'm using french canadian
      > regional options if it can gives you a hint. And if it is really
      > related to regional and language options, what should i change
      > exactly?
      >
      > If anyone has an idea how to make it works with a dot that would be
      > great. I want understand why it doesn't works. Thanks![/color]

      Try:
      double.Parse("2 0.50", new System.Globaliz ation.CultureIn fo("en-US"));

      And yes, it's about regional options... :)


      Comment

      • Matt

        #4
        Re: Double conversion problem

        Well it was the regional settings. I just set it to Canadian English
        and it works fine for both.

        However it means that this section of code will depend of the user's
        regional settings. Fortunately for me it's an asp.net project that
        will run on a specific server. But if it was a c# program made for
        deployment on different computers, that would be problematic.

        Comment

        • Scott

          #5
          Re: Double conversion problem

          Matt,

          Everyone has this number from string parse problem. As an American
          living in Europe, with a french computer programing for the US, I got
          to it quicker. If you need to reliably convert a number to and from a
          string, there is nothing better than "forcing" the culture to
          CultureInfo.Inv ariantCulture. You will find it (if not exactly) very
          similar to en-US. so parsing would go like:
          double dbl = double.Parse("1 234.56",
          System.Globaliz ation.CultureIn fo.InvariantCul ture);

          And when you need to get your string from the double, you can do the
          same thing in "reverse":
          string s =
          dbl.ToString(Sy stem.Globalizat ion.CultureInfo .InvariantCultu re);

          Scott

          "Matt" <metal@rocks.co m> wrote in message
          news:s92eivk32e nlhqqmooo3h7via tp0be2g94@4ax.c om...[color=blue]
          > Well it was the regional settings. I just set it to Canadian English
          > and it works fine for both.
          >
          > However it means that this section of code will depend of the user's
          > regional settings. Fortunately for me it's an asp.net project that
          > will run on a specific server. But if it was a c# program made for
          > deployment on different computers, that would be problematic.[/color]


          Comment

          Working...