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. How to Convert a Float to a string

How to Convert a Float to a string

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
10 Posts 8 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.
  • S Offline
    S Offline
    saravana001
    wrote on last edited by
    #1

    Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks

    N C V J D 5 Replies Last reply
    0
    • S saravana001

      Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks

      N Offline
      N Offline
      Nelek
      wrote on last edited by
      #2

      itoa is only between int and char. if you want float or double try dtostr / strtod And still better, if you are using CString, then use CString::Format CString szValue = ""; szValue.Format (_T("%f"), doubleVariable); This works with hexadecimal, int, binary... take a look in documentation.

      Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you ;)

      1 Reply Last reply
      0
      • S saravana001

        Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        CString t; t.Format("%f", floatVal);

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • S saravana001

          Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks

          V Offline
          V Offline
          VonHagNDaz
          wrote on last edited by
          #4

          the result from google search "c++ float to string" give this example... int main() { char* str = new char[30]; float flt = 2.4567F; sprintf(str, "%.4g", flt ); cout< [Insert Witty Sig Here]

          J T 2 Replies Last reply
          0
          • S saravana001

            Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #5

            Besides all those Old-School C-Ways of converting, there is the std::stringstream:

            #include <strstream>
            #include <iostream>

            const double pi = 3.14; //Feel free to a digits at will
            std::string piString;

            int main(int argc, char * argv[])
            {
            std::stringstream stream;
            stream << pi;
            //put any number of formatters here
            stream >> piString;

            std::cout << piString;

            return system("pause");
            }

            Also, boost::lexical_cast[^] comes to mind.


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            1 Reply Last reply
            0
            • S saravana001

              Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks

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

              saravananmechanical wrote:

              char TempBuffer[50];

              Why are you using an intermediary variable? Convert i directly into str.


              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              1 Reply Last reply
              0
              • V VonHagNDaz

                the result from google search "c++ float to string" give this example... int main() { char* str = new char[30]; float flt = 2.4567F; sprintf(str, "%.4g", flt ); cout< [Insert Witty Sig Here]

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                Please tell us the site where you got that from so that it can be blacklisted!  Or at least the original author so other developers here can watch out for their name on a resume...    It is stupid enough to use new to allocate 30 bytes that are never used beyond the scope of allocation.  It is also pretty dumb not to delete[] them.  It is beyond my understanding why someone would make such code public.  I mean, come on, at least show stuff like this to your friends first...    Oh, and the %g specifier is technically for doubles, not floats. :)   And mixes C and C++ style output (could format using the stream), etc... :suss:    Peace!

                -=- James
                Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                See DeleteFXPFiles

                V 1 Reply Last reply
                0
                • J James R Twine

                  Please tell us the site where you got that from so that it can be blacklisted!  Or at least the original author so other developers here can watch out for their name on a resume...    It is stupid enough to use new to allocate 30 bytes that are never used beyond the scope of allocation.  It is also pretty dumb not to delete[] them.  It is beyond my understanding why someone would make such code public.  I mean, come on, at least show stuff like this to your friends first...    Oh, and the %g specifier is technically for doubles, not floats. :)   And mixes C and C++ style output (could format using the stream), etc... :suss:    Peace!

                  -=- James
                  Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  See DeleteFXPFiles

                  V Offline
                  V Offline
                  VonHagNDaz
                  wrote on last edited by
                  #8

                  http://www.thescripts.com/forum/thread550915.html

                  [Insert Witty Sig Here]

                  D 1 Reply Last reply
                  0
                  • V VonHagNDaz

                    http://www.thescripts.com/forum/thread550915.html

                    [Insert Witty Sig Here]

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

                    You just as easily could have taken Google's second or third result and been much better off.


                    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    1 Reply Last reply
                    0
                    • V VonHagNDaz

                      the result from google search "c++ float to string" give this example... int main() { char* str = new char[30]; float flt = 2.4567F; sprintf(str, "%.4g", flt ); cout< [Insert Witty Sig Here]

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #10

                      VonHagNDaz wrote:

                      [Insert Witty Sig Here]

                      waiting for something to insert [:)]

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                      Never mind - my own stupidity is the source of every "problem" - Mixture

                      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                      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