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. std::string to float

std::string to float

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
11 Posts 9 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
    rahultaing
    wrote on last edited by
    #1

    can anyone tell me how to convert a std::string to float type?

    C M 2 Replies Last reply
    0
    • R rahultaing

      can anyone tell me how to convert a std::string to float type?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      float MyFloat = atof(MyString.c_str());

      J 1 Reply Last reply
      0
      • R rahultaing

        can anyone tell me how to convert a std::string to float type?

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        R 1 Reply Last reply
        0
        • M Michael Dunn

          From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

          R Offline
          R Offline
          Roland Pibinger
          wrote on last edited by
          #4

          Michael Dunn wrote:

          From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]

          Please no stringstream solution. Just encapsulate strtod.

          M L 2 Replies Last reply
          0
          • R Roland Pibinger

            Michael Dunn wrote:

            From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]

            Please no stringstream solution. Just encapsulate strtod.

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            :confused: Sorry for providing a pointer to a FAQ with a solution to the OP's problem.

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

            L D 2 Replies Last reply
            0
            • M Michael Dunn

              :confused: Sorry for providing a pointer to a FAQ with a solution to the OP's problem.

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Michael Dunn wrote:

              OP's

              means "Original Poster" ? led mike

              1 Reply Last reply
              0
              • R Roland Pibinger

                Michael Dunn wrote:

                From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]

                Please no stringstream solution. Just encapsulate strtod.

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

                Roland Pibinger wrote:

                Please no stringstream solution.

                Out of interest, why not?

                S 1 Reply Last reply
                0
                • L Lost User

                  Roland Pibinger wrote:

                  Please no stringstream solution.

                  Out of interest, why not?

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  I would like to know why not too. While MSVC6s STL implementation is crap and it would be more efficient to use the old C-style functions, other STL versions are more efficient and have less overhead. Efficiency issues aside, there are many advantages to the stringstream solutions such as that fact that it's extensible and you can change the type your converting to simply by changing variable your reading into instead of having to change the function called. The intent is also clearer as the C style function names are obscure to say the least. Another issue is the fact that STL implementations are allowed to implement basic_string in a manner such that calling c_str is not as efficient as the programmer might think. One example is basic_string implementations which don’t use a single contiguous memory block to store the string. Steve -- modified at 21:50 Thursday 4th May, 2006

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    float MyFloat = atof(MyString.c_str());

                    J Offline
                    J Offline
                    jiangalong
                    wrote on last edited by
                    #9

                    float MyFloat=atoi(MyString.c_str()); That's correct!;P

                    C 1 Reply Last reply
                    0
                    • J jiangalong

                      float MyFloat=atoi(MyString.c_str()); That's correct!;P

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      :confused: Err, no, this will convert a string to an integer value, not to a float value. atof convert to a float value.

                      1 Reply Last reply
                      0
                      • M Michael Dunn

                        :confused: Sorry for providing a pointer to a FAQ with a solution to the OP's problem.

                        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Gee Mike, what in the world were you thinking? :rolleyes:


                        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                        "We will be known forever by the tracks we leave." - Native American Proverb

                        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