Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem with converting to Double

problem with converting to Double

Scheduled Pinned Locked Moved C#
helptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    Dear all, I have a little problem converting a string that contains a ',' into a double. Double needs a value devided by a dot ( or so I've read ) So , in my code , the following occurs :

    string lat = p[u].X.ToString().Replace(',', '.');
    MessageBox.Show(lat);

    The output will be for example : 55.38978 ( which shows up perfect in the messagebox ) However, if i do the following :

    MessageBox.Show(Convert.ToDouble(lat).ToString());

    The output will be : 5538978 That doesn't make a whole lot of sense to me. So that dot is somehow bothering Double or am I not converting properly ? Cheers :)

    D L 2 Replies Last reply
    0
    • R Rick van Woudenberg

      Dear all, I have a little problem converting a string that contains a ',' into a double. Double needs a value devided by a dot ( or so I've read ) So , in my code , the following occurs :

      string lat = p[u].X.ToString().Replace(',', '.');
      MessageBox.Show(lat);

      The output will be for example : 55.38978 ( which shows up perfect in the messagebox ) However, if i do the following :

      MessageBox.Show(Convert.ToDouble(lat).ToString());

      The output will be : 5538978 That doesn't make a whole lot of sense to me. So that dot is somehow bothering Double or am I not converting properly ? Cheers :)

      D Offline
      D Offline
      Deresen
      wrote on last edited by
      #2

      Well the problem is that double works with a komma in stead of a dot. See it like this:

              string ss = "55,5493";
              string lat = ss.ToString().Replace(',', '.');
              double d = Convert.ToDouble(ss);
              double dd = Convert.ToDouble(lat);
              MessageBox.Show("" + d); //output: 55,5493
              MessageBox.Show("" + dd); //oubput: 555493
      

      So you should change this one:

      string lat = ss.ToString().Replace(',', '.');

      into this one:

      string lat = ss.ToString().Replace('.', ',');

      Good luck

      1 Reply Last reply
      0
      • R Rick van Woudenberg

        Dear all, I have a little problem converting a string that contains a ',' into a double. Double needs a value devided by a dot ( or so I've read ) So , in my code , the following occurs :

        string lat = p[u].X.ToString().Replace(',', '.');
        MessageBox.Show(lat);

        The output will be for example : 55.38978 ( which shows up perfect in the messagebox ) However, if i do the following :

        MessageBox.Show(Convert.ToDouble(lat).ToString());

        The output will be : 5538978 That doesn't make a whole lot of sense to me. So that dot is somehow bothering Double or am I not converting properly ? Cheers :)

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi Rick, ToString() when applied to numbers will by default use the regional settings of your system, which define a "decimal point" (not necessarily a period) and a "thousands separator" (not necessarily a comma). Parse() and TryParse() will observe the "decimal point" and ignore the "thousands separator". If you have a number formatted one way (say West-European way, with decimal comma, and thousands period) and parsed another way (say US way), you will get wrong results, and even an Exception when more than one thousands separator is present. The solution is NOT to replace periods by comma and vice versa, the solution is to apply the appropriate settings, by using a FormatProvider or CultureInfo. The above applies not only to doubles, but also to floats and decimals. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups