Parse a textBox to Double not working properly. Very strange.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Promedeus
    New Member
    • Mar 2010
    • 3

    Parse a textBox to Double not working properly. Very strange.

    Hello,
    I am incredibly puzzled.
    I have made a small, fairly simple calculator for specific purposes. I can run this fine on my computer. But when I try to run it on a target computer (from Norway) I get an error that puzzles me.

    Here's the strange behavoir:

    Code:
    textBox.Text = "1";
    double result = double.Parse(textBox.Text)  * .15;
    This piece of code works fine on both computers, and with out a dought should.

    This is where it get's wierd:
    Code:
    textBox.Text = "1.1";
    double result = double.Parse(textBox.Text)  * .15;
    This doesn't work on a computer in Norway, but on all the computers I have tried that I can access it works fine.

    So the problem is converting a String to a Double.
    More specifcaly this piece of code:
    Code:
    double.Parse(textBox.Text)
    When
    Code:
    textBox.Text = "1.1";
    Even though there should be no problem.

    I have coded the calculator to where you can only enter valid characters to the textBox. (numbers and one decimal are allowed)

    When I get the error from the computer in Norway, it's telling me that I can't convert "1.1" in the textBox to a Double. Which should work, but for some odd reason doesn't. I've tried a lot of differant things like taking the "1.1" from the textBox and putting it in a string, then trying to parse that, same error. So what I'm wonering is if there might be some type of language conversion problem, or something buggy with that computer?

    Both computers have Win7, and the latest .NET framework. Which reminds me that I have used the 4.0 Beta framework to program this, so what I also tried was re-writing for 3.5. Which also gave me the same error.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    The decimal marker in Norway is a comma, not a period.

    1,1

    Comment

    • Promedeus
      New Member
      • Mar 2010
      • 3

      #3
      Thanks, I'll give it a try!

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Yes, these types of issues are almost always related to a difference in language/locale. Different settings use different characters as decimal seperators, thousands seperators, and etc.
        If your application is going to be used globally, you will need to do a lot more research into locales :-(

        Comment

        • Abdel Rochdi

          #5
          Thank you so much "tlhintoq" and thanks to the asker of this question too. This problem was puzzling me for a week, and I scoured Google for how to convert a String to a Double, every time I write Double.Parse(te xt) it does not work when I run a preview of my Software. My OS is English, but I am developing my Software in a virtual French OS and when I type a period (.) as a decimal it bugs and gives me error. Darn it, a week of mind puzzle, and I even paused my Software for over 2 weeks. Thank you again for your valuable help. I am relocating to my English OS :)

          Comment

          • enty
            New Member
            • Feb 2012
            • 1

            #6
            For example you can use this

            Code:
            string numSeparator = 
            CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
            char numSepFromThis = (char)0;
            char numSepToThis = (char)0;
            if (numSeparator == ".") 
            {
                 numSepFromThis = ',';
                 numSepToThis = '.';
            }
            else if (numSeparator == ",")
            {
                 numSepFromThis = '.';
                 numSepToThis = ',';
            }
            to text

            Code:
            textBox.Text.Trim().Replace(numSepFromThis,numSepToThis);

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Wow, that's a huge thread necro :D Thanks for posting the tip.

              Also, it's worthwhile pointing out that the Parse method (as well as TryParse, I believe) will accept a CultureInfo object. For example...

              Code:
                          double d1 = double.Parse("3.14", Thread.CurrentThread.CurrentCulture);
                          double d2 = double.Parse("3,14", new CultureInfo("nb-NO"));
              
                          Console.WriteLine(d1.ToString());
                          Console.WriteLine(d2.ToString());
              Your code would need to use the System.Globaliz ation and System.Threadin g namespaces to access this code. The first one will parse "3.14" with whatever the current thread's culture is. In my case it would be "en-US". The second parses with the Norwegian culture and correctly parses "3,14" to a float value of 3.14.

              Hopefully that helps someone out there!

              Comment

              Working...