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 / C++ / MFC
  4. convertion between string and double

convertion between string and double

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 5 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.
  • C Offline
    C Offline
    Chrissie ja
    wrote on last edited by
    #1

    I have a problem with convertion between string and double as following: double dValue = 1000; std::ostrstream oStrStream; oStrStream << dValue << std::ends; std::string strValue = oStrStream.str(); //here strValue is "1,000", a comma is added! //when I use following code to convert, error occurred. double dValue = strtod( strValue .c_str(), NULL ); //here dValue is "1", it seems "," can't be recognized! How to avoid this? Is there any way to avoid adding comma when convert double to string?

    _ L 2 Replies Last reply
    0
    • C Chrissie ja

      I have a problem with convertion between string and double as following: double dValue = 1000; std::ostrstream oStrStream; oStrStream << dValue << std::ends; std::string strValue = oStrStream.str(); //here strValue is "1,000", a comma is added! //when I use following code to convert, error occurred. double dValue = strtod( strValue .c_str(), NULL ); //here dValue is "1", it seems "," can't be recognized! How to avoid this? Is there any way to avoid adding comma when convert double to string?

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      ostrstream is not supported anymore. Try using ostringstream;

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      C 1 Reply Last reply
      0
      • _ _Superman_

        ostrstream is not supported anymore. Try using ostringstream;

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        C Offline
        C Offline
        Chrissie ja
        wrote on last edited by
        #3

        I got the same result. The problem was not solved yet.

        K 1 Reply Last reply
        0
        • C Chrissie ja

          I have a problem with convertion between string and double as following: double dValue = 1000; std::ostrstream oStrStream; oStrStream << dValue << std::ends; std::string strValue = oStrStream.str(); //here strValue is "1,000", a comma is added! //when I use following code to convert, error occurred. double dValue = strtod( strValue .c_str(), NULL ); //here dValue is "1", it seems "," can't be recognized! How to avoid this? Is there any way to avoid adding comma when convert double to string?

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

          You need to parse the string to remove non-numeric characters before trying to convert it. The string is only allowed to follow a specific pattern, as described here[^].

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          A 1 Reply Last reply
          0
          • C Chrissie ja

            I got the same result. The problem was not solved yet.

            K Offline
            K Offline
            KingsGambit
            wrote on last edited by
            #5

            The problem is due to ',' in '1,000'. Remove ',' and it would work.

            1 Reply Last reply
            0
            • L Lost User

              You need to parse the string to remove non-numeric characters before trying to convert it. The string is only allowed to follow a specific pattern, as described here[^].

              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

              A Offline
              A Offline
              Addy Tas
              wrote on last edited by
              #6

              I could be wrong here, didn't verify yet, but using the setlocale to LC_NUMERIC for US English shoul make it recognize the thousand seperator as well. Always consider that there are languages (mine included) that use a different decimal seperator. Cheers, AT

              Cogito ergo sum

              L 1 Reply Last reply
              0
              • A Addy Tas

                I could be wrong here, didn't verify yet, but using the setlocale to LC_NUMERIC for US English shoul make it recognize the thousand seperator as well. Always consider that there are languages (mine included) that use a different decimal seperator. Cheers, AT

                Cogito ergo sum

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

                If you look at the link I provided you can see which characters may be accepted.

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                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