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. string conversion to double

string conversion to double

Scheduled Pinned Locked Moved C#
tutorialquestion
11 Posts 4 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.
  • A Offline
    A Offline
    Aljaz111
    wrote on last edited by
    #1

    how to convert string like 0.5555 to rounded double value of 0.6 in this case? Because when i use Convert.ToDouble("0.555555") the result is 55555555.. Thanks

    L D 2 Replies Last reply
    0
    • A Aljaz111

      how to convert string like 0.5555 to rounded double value of 0.6 in this case? Because when i use Convert.ToDouble("0.555555") the result is 55555555.. Thanks

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

      Hi, the Convert class (or the Parse methods of several classes) will turn a string into a numeric value that matches the string as good as it can. There is no way around that. You can change the result by adding another statement that operates on the numeric variable, something like double d=Math.Round(d,1);. AFAIK there isn't a single statement that will do both at once. read up on the Math.Round() method. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Happy New Year to all.
      We hope 2010 soon brings us automatic PRE tags!
      Until then, please insert them manually.


      A 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, the Convert class (or the Parse methods of several classes) will turn a string into a numeric value that matches the string as good as it can. There is no way around that. You can change the result by adding another statement that operates on the numeric variable, something like double d=Math.Round(d,1);. AFAIK there isn't a single statement that will do both at once. read up on the Math.Round() method. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Happy New Year to all.
        We hope 2010 soon brings us automatic PRE tags!
        Until then, please insert them manually.


        A Offline
        A Offline
        Aljaz111
        wrote on last edited by
        #3

        Still..you can't convert string 0.555 to double value 0.555? Always removes 0 from first place..i used another more complicated way..i separate string..i get second decimal..check if it is greater than 5..if yes i inc first decimal and at the end i put numbers converted to string back together.

        D L 2 Replies Last reply
        0
        • A Aljaz111

          Still..you can't convert string 0.555 to double value 0.555? Always removes 0 from first place..i used another more complicated way..i separate string..i get second decimal..check if it is greater than 5..if yes i inc first decimal and at the end i put numbers converted to string back together.

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Aljaz111 wrote:

          you can't convert string 0.555 to double value 0.555

          Of course you can, just use double.Parse or better still TryParse

          double d = double.Parse("0.555");

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          1 Reply Last reply
          0
          • A Aljaz111

            Still..you can't convert string 0.555 to double value 0.555? Always removes 0 from first place..i used another more complicated way..i separate string..i get second decimal..check if it is greater than 5..if yes i inc first decimal and at the end i put numbers converted to string back together.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Aljaz111 wrote:

            Always removes 0 from first place..

            Is the decimal separator in your country by any chance a comma? How about this for verification;

            string s = "0.55555";
            double d = Convert.ToDouble(s, System.Globalization.CultureInfo.InvariantCulture);
            string s2 = d.ToString("#,#0.0");
            Console.WriteLine(s2);

            I are Troll :suss:

            A D 2 Replies Last reply
            0
            • A Aljaz111

              how to convert string like 0.5555 to rounded double value of 0.6 in this case? Because when i use Convert.ToDouble("0.555555") the result is 55555555.. Thanks

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              I'm not quite sure how you're approching this but it's quite simple

              public static double StringToRoundedDouble(string s, int decimals)
              {
              double result;
              if (double.TryParse(s, out result))
              result = Math.Round(result, decimals, MidpointRounding.AwayFromZero);
              return result;
              }

              Useage

              double d =(StringToRoundedDouble("0.5555", 1);

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              1 Reply Last reply
              0
              • L Lost User

                Aljaz111 wrote:

                Always removes 0 from first place..

                Is the decimal separator in your country by any chance a comma? How about this for verification;

                string s = "0.55555";
                double d = Convert.ToDouble(s, System.Globalization.CultureInfo.InvariantCulture);
                string s2 = d.ToString("#,#0.0");
                Console.WriteLine(s2);

                I are Troll :suss:

                A Offline
                A Offline
                Aljaz111
                wrote on last edited by
                #7

                it is. Thanks or info

                1 Reply Last reply
                0
                • L Lost User

                  Aljaz111 wrote:

                  Always removes 0 from first place..

                  Is the decimal separator in your country by any chance a comma? How about this for verification;

                  string s = "0.55555";
                  double d = Convert.ToDouble(s, System.Globalization.CultureInfo.InvariantCulture);
                  string s2 = d.ToString("#,#0.0");
                  Console.WriteLine(s2);

                  I are Troll :suss:

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Good thinking :thumbsup:

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                  L 1 Reply Last reply
                  0
                  • D DaveyM69

                    Good thinking :thumbsup:

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Thanks :-D

                    I are Troll :suss:

                    A 1 Reply Last reply
                    0
                    • L Lost User

                      Thanks :-D

                      I are Troll :suss:

                      A Offline
                      A Offline
                      Aljaz111
                      wrote on last edited by
                      #10

                      Ok it doesn't work without System.Globalization.CultureInfo.InvariantCulture but with it in Convert.Double(s, System.Globalization.CultureInfo.InvariantCulture) works just fine. Can you explain me please what does this have to do with rounding numbers? Thx

                      L 1 Reply Last reply
                      0
                      • A Aljaz111

                        Ok it doesn't work without System.Globalization.CultureInfo.InvariantCulture but with it in Convert.Double(s, System.Globalization.CultureInfo.InvariantCulture) works just fine. Can you explain me please what does this have to do with rounding numbers? Thx

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        It interprets the decimal point wrong, if you replace the point with a comma then all goes well. There's also a System.Globalization.CultureInfo.CurrentCulture. The invariant culture will use the decimal-point, no matter what's been set currently by the user. If you convert the double "2.0" from invariant to your CurrentCulture[^], then you'd probably get "2,0" :)

                        I are Troll :suss:

                        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