Results 1 to 6 of 6

Thread: [RESOLVED] float.Parse() should always take "." as decimal seperator.

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Resolved [RESOLVED] float.Parse() should always take "." as decimal seperator.

    Hi,


    I am trying the following

    String str = "1.3";
    float f = float.Parse(str);

    On an american system this will work perfectly.
    result: f = 1.3

    However in some european countries "." is not the decimal seperator but "," is the decimal seperator.
    result: f = 13

    I need to get this code running on all platforms though. I could ofcourse just replace the "," and "." signs. But then it would no longer run on american systems.

    How is this problem normally fixed? In my program "." should always be seen as a decimal seperator.

    This is important because I am reading files with coordinates that need to be parsed correctly. In these files (*.x3d) the "." sign is the decimal seperator.

    Thanks in advance for all help!
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: float.Parse() should always take "." as decimal seperator.

    You use ParseExact if you want to specify an absolute format. I think that should work in this case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: float.Parse() should always take "." as decimal seperator.

    Thanks for your quick reply!

    Strange no such class or function with name "ParseExact".

    Hmm, I'll try to write a class that implements IFormatProvider in mean time.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  4. #4

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: float.Parse() should always take "." as decimal seperator.

    Code:
    System.Globalization.NumberFormatInfo nmbrstyle = new System.Globalization.NumberFormatInfo();
    nmbrstyle.NumberDecimalSeparator = ".";
    nmbrstyle.NumberGroupSeparator = ",";
    float.Parse(str, nmbrstyle);
    I'll try with this. Maybe this works. NumberFormatInfo seems to already implement IFormatProvider.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  5. #5
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Re: float.Parse() should always take "." as decimal seperator.

    hi, take a look at this

    Code:
    CultureInfo currentCulture = new CultureInfo("de-DE");
    Thread.CurrentThread.CurrentUICulture = currentCulture;
    Console.WriteLine(currentCulture.NumberFormat.NumberDecimalSeparator);
    
    string strDouble = "3,4";
    
    double dblDouble = Double.Parse(strDouble,currentCulture.NumberFormat);
    
    Console.WriteLine(dblDouble);
    This produces the correct result...
    You dont need eyes to see, you need vision.

  6. #6

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: float.Parse() should always take "." as decimal seperator.

    Thanks for all your replies.
    But in fact the last code I posted worked perfectly.

    This thread is resolved.

    Thanks!
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

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