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 transfer Cstring to float

how to transfer Cstring to float

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
9 Posts 6 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
    camuoi288
    wrote on last edited by
    #1

    How to transfer Cstring type like : 6.591E+02 to float type. exmple : Cstring a="6.591E+02"; float b; and i want b=6.591E+02.I work with MFC.Thanks.

    R C N 3 Replies Last reply
    0
    • C camuoi288

      How to transfer Cstring type like : 6.591E+02 to float type. exmple : Cstring a="6.591E+02"; float b; and i want b=6.591E+02.I work with MFC.Thanks.

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      See atof()[^]

      "Real men drive manual transmission" - Rajesh.

      C 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        See atof()[^]

        "Real men drive manual transmission" - Rajesh.

        C Offline
        C Offline
        camuoi288
        wrote on last edited by
        #3

        I also used atof(),but with atof() parameter in atof()function is char * but my string is Cstring.:(

        R V H 3 Replies Last reply
        0
        • C camuoi288

          I also used atof(),but with atof() parameter in atof()function is char * but my string is Cstring.:(

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          If you're doing a Unicode build, you could use _ttof() and otherwise atof() will work. For example, this code will work for an MBCS build:

          double df = 0;
          CString str = "6.591E+02";
          df = atof(str);

          Hint: CString has the LPCTSTR operator defined, so the conversion will be done automatically.

          "Real men drive manual transmission" - Rajesh.

          1 Reply Last reply
          0
          • C camuoi288

            I also used atof(),but with atof() parameter in atof()function is char * but my string is Cstring.:(

            V Offline
            V Offline
            venkatmakam
            wrote on last edited by
            #5

            Let your variable is

            CString strFloat;

            then use atof as

            atof(strFloat.GetBuffer());

            R 1 Reply Last reply
            0
            • V venkatmakam

              Let your variable is

              CString strFloat;

              then use atof as

              atof(strFloat.GetBuffer());

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              That's a misuse of CString::GetBuffer().

              "Real men drive manual transmission" - Rajesh.

              1 Reply Last reply
              0
              • C camuoi288

                How to transfer Cstring type like : 6.591E+02 to float type. exmple : Cstring a="6.591E+02"; float b; and i want b=6.591E+02.I work with MFC.Thanks.

                C Offline
                C Offline
                Cool_Dev
                wrote on last edited by
                #7

                CString a = "6.591E+02";
                float b;
                sscanf(a,"%E",&b); //use _stscanf() for unicode suppport

                CString a = "6.591E+02";
                float b;
                b = atof(a); // use _ttof() for unicode support

                modified on Wednesday, April 20, 2011 6:03 AM

                1 Reply Last reply
                0
                • C camuoi288

                  I also used atof(),but with atof() parameter in atof()function is char * but my string is Cstring.:(

                  H Offline
                  H Offline
                  Hans Dietrich
                  wrote on last edited by
                  #8

                  camuoi288 wrote:

                  atof() parameter in atof()function is char * but my string is Cstring

                  That is not correct. From MSDN:

                  double atof(const char \*string);
                  

                  Since CString has LPCTSTR operator, you can easily call atof like this:

                  CString str = "1.234";
                  double d = atof(str);
                  

                  Best wishes, Hans


                  [Hans Dietrich Software]

                  1 Reply Last reply
                  0
                  • C camuoi288

                    How to transfer Cstring type like : 6.591E+02 to float type. exmple : Cstring a="6.591E+02"; float b; and i want b=6.591E+02.I work with MFC.Thanks.

                    N Offline
                    N Offline
                    Niklas L
                    wrote on last edited by
                    #9

                    Since you're using C++ I suggest using streams.

                    #include <sstream>
                    {
                    CString strFloat("3.5");
                    istringstream is(strFloat);
                    float myFloat;
                    is >> myFloat;
                    }

                    This way you can catch errors easily. Consider the following:

                    float af = atof("a");
                    float zerof = atof("0");

                    Both af and zerof will be 0, since on error atof() will return 0. Using streams you can check the stream status after conversion:

                    {
                    CString strFloat("3.5");
                    istringstream is(strFloat);
                    float myFloat;
                    is >> myFloat;
                    if (is.fail())
                    {
                    // Conversion failed!
                    }
                    }

                    home

                    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