CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2005
    Posts
    66

    Float conversion problem

    Hi,

    as I found out C# does conversion of float strings in different ways on different OS (e.g. english WindowsXP and german WindowsXP).

    For example if I execute:
    Code:
    string st = "100.00";
    float fl = (float)System.Convert.ToDouble(st);
    MessageBox.Show( st + "-->" + fl.ToString() );
    
    st = "37,00";
    fl = (float)System.Convert.ToDouble(st);
    MessageBox.Show( st + "-->" + fl.ToString() );
    I get "10000" and "37" for the converted floats on the german OS. However if I execute the same stripped of code on an english Windows it converts to "100" and "3700".
    I can imagine what went wrong, but I don't know how to fix that issue. The question is: how to tell the application what to use as a float separator no matter what kind of language is used, because one can assume that internally all float strings are handled in "12.345" format.

    Thanks in advance,
    gbr

  2. #2
    Join Date
    Jul 1999
    Location
    Malmö, Sweden
    Posts
    126

    Re: Float conversion problem

    Your question is excellently answered by MSDN. Look up System.Convert class and read about System.Convert.ToDouble(string, IFormatProvider). In short it says that you can pass an instance of NumberFormatInfo as IFormatProvider. NumberFormatInfo has members such as NumberDecimalSeparator.

    HTH!

  3. #3
    Join Date
    Jul 1999
    Location
    Malmö, Sweden
    Posts
    126

    Re: Float conversion problem

    Oh, and by the way, I don't think different languages of XP is the issue, rather it is differences in Regional Settings. When you are converting user input you should use the overload of ToDouble in your post, not the one I suggested, because as I understand it your overload uses formatting info from Regional Settings. This way the user has the option to choose any format he/she likes of floating numbers and you will be able to convert them successfully.
    Cheers.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Float conversion problem

    Actually, its a bit wierd for me. The convert should basically fail because there's a comma (,) in the string !! Anyways, what Anders says is correct.
    Quote Originally Posted by Anders
    I don't think different languages of XP is the issue, rather it is differences in Regional Settings.
    The regional setting are there in the windows registry. It picks up the basic formats from there - for dates, currencies etc. Try using the overloads specifying the formats. By the way, it makes sense, since you are using a German Version of the OS..the inputs you would be providing would be as per your regional settings. So, in a way that is correct and part of "Localization" of your application according to the .Net framework. However, if you want a specific way, go as suggested by Anders.

  5. #5
    Join Date
    Jun 2005
    Posts
    66

    Re: Float conversion problem

    Thank you. Now I am using
    Code:
    System.Globalization.NumberFormatInfo nfi = 
    new System.Globalization.CultureInfo( "en-US", false ).NumberFormat;
    fl = (float)System.Convert.ToDouble( st, nfi );
    which converts to the same float without caring about region settings.

    gbr

  6. #6
    Join Date
    Jul 1999
    Location
    Malmö, Sweden
    Posts
    126

    Re: Float conversion problem

    Quote Originally Posted by exterminator
    The convert should basically fail because there's a comma (,) in the string !!
    Not neccessarily, as I understand it. My current Regional Settings has a comma as the grouping character and thus it is a legal characters that is just discarded when converting. I think. Not sure :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured