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. Howto convert CString to float?

Howto convert CString to float?

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 7 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.
  • V Offline
    V Offline
    VanHlebar
    wrote on last edited by
    #1

    How do I convert a CString to a float without losing any precision? When I do the following I lose precision. CString strFloat = _T("0.5"); char buf[20]; for(int i=0; i<20; i++) buf[i] = NULL; sprintf(buf, "%s", strFloat); float nResult = atof(buf); Thanks, -Eric

    A 1 Reply Last reply
    0
    • V VanHlebar

      How do I convert a CString to a float without losing any precision? When I do the following I lose precision. CString strFloat = _T("0.5"); char buf[20]; for(int i=0; i<20; i++) buf[i] = NULL; sprintf(buf, "%s", strFloat); float nResult = atof(buf); Thanks, -Eric

      A Offline
      A Offline
      ashxly
      wrote on last edited by
      #2

      you can do it like this CString str(_T("0.5")); float fResult = atof(str); :((

      V 1 Reply Last reply
      0
      • A ashxly

        you can do it like this CString str(_T("0.5")); float fResult = atof(str); :((

        V Offline
        V Offline
        VanHlebar
        wrote on last edited by
        #3

        ashxly wrote: you can do it like this CString str(_T("0.5")); float fResult = atof(str); Nope, get a compile error i I try that. I have also tried _wtoi and _wtol but they lose precision also. -Eric

        V G A R 4 Replies Last reply
        0
        • V VanHlebar

          ashxly wrote: you can do it like this CString str(_T("0.5")); float fResult = atof(str); Nope, get a compile error i I try that. I have also tried _wtoi and _wtol but they lose precision also. -Eric

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

          Consider atof(theString.GetBuffer(0)). Kuphryn

          V 1 Reply Last reply
          0
          • V VanHlebar

            ashxly wrote: you can do it like this CString str(_T("0.5")); float fResult = atof(str); Nope, get a compile error i I try that. I have also tried _wtoi and _wtol but they lose precision also. -Eric

            G Offline
            G Offline
            Gary Kirkham
            wrote on last edited by
            #5

            What error are you getting? The code provided by ashxly should work. You should get a compiler warning that says warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data That is because atof returns a double...the warning will go away if you change float fResult to double fResult. OR cast the return value from atof to float. _wtoi and _wtol are no good for your purpose...they will both truncate to an integer. Gary Kirkham A working Program is one that has only unobserved bugs

            1 Reply Last reply
            0
            • V valikac

              Consider atof(theString.GetBuffer(0)). Kuphryn

              V Offline
              V Offline
              VanHlebar
              wrote on last edited by
              #6

              kuphryn wrote: Consider atof(theString.GetBuffer(0)). Nope, I get the same error with this. Can't convert CString to const char *. There has to be a way to convert a CString to a float without losing the precision. It shouldn't be this hard I wouldn't have thought. :( -Eric

              V 1 Reply Last reply
              0
              • V VanHlebar

                kuphryn wrote: Consider atof(theString.GetBuffer(0)). Nope, I get the same error with this. Can't convert CString to const char *. There has to be a way to convert a CString to a float without losing the precision. It shouldn't be this hard I wouldn't have thought. :( -Eric

                V Offline
                V Offline
                valikac
                wrote on last edited by
                #7

                Consider const_cast. Kuphryn

                1 Reply Last reply
                0
                • V VanHlebar

                  ashxly wrote: you can do it like this CString str(_T("0.5")); float fResult = atof(str); Nope, get a compile error i I try that. I have also tried _wtoi and _wtol but they lose precision also. -Eric

                  A Offline
                  A Offline
                  ashxly
                  wrote on last edited by
                  #8

                  CString str(_T("0.5")); float fResult = atof(str); the code work well. certainly , if you use UNICODE, you'd better use wtof(str) instead can you tell me the error info?:-O

                  1 Reply Last reply
                  0
                  • V VanHlebar

                    ashxly wrote: you can do it like this CString str(_T("0.5")); float fResult = atof(str); Nope, get a compile error i I try that. I have also tried _wtoi and _wtol but they lose precision also. -Eric

                    R Offline
                    R Offline
                    Rage
                    wrote on last edited by
                    #9

                    What about

                    float fResult=atof((LPCTSTR)str);

                    ~RaGE();

                    X V 2 Replies Last reply
                    0
                    • R Rage

                      What about

                      float fResult=atof((LPCTSTR)str);

                      ~RaGE();

                      X Offline
                      X Offline
                      xxhimanshu
                      wrote on last edited by
                      #10

                      ;);)Rage is right This should dp the work work. cheers Himanshu

                      1 Reply Last reply
                      0
                      • R Rage

                        What about

                        float fResult=atof((LPCTSTR)str);

                        ~RaGE();

                        V Offline
                        V Offline
                        VanHlebar
                        wrote on last edited by
                        #11

                        Rage wrote: float fResult=atof((LPCTSTR)str); This gives the following error: error C2664: 'atof' : cannot convert parameter 1 from 'const unsigned short *' to 'const char *' I apologize ahead of time if this makes a difference, but I am compiling this under PocketPC 2002 SDK. After working somemore lastnight I figured out part of the issue. In my orginal code the call to sprintf() is always returning zero into buf. It works just fine if the user has an whole number in strFloat, but once they enter a decimal number it blowsup, only returning zero into buf. Thanks for all the help folks. :) I honestly didn't think this would be that difficult.:-O -Eric

                        T 1 Reply Last reply
                        0
                        • V VanHlebar

                          Rage wrote: float fResult=atof((LPCTSTR)str); This gives the following error: error C2664: 'atof' : cannot convert parameter 1 from 'const unsigned short *' to 'const char *' I apologize ahead of time if this makes a difference, but I am compiling this under PocketPC 2002 SDK. After working somemore lastnight I figured out part of the issue. In my orginal code the call to sprintf() is always returning zero into buf. It works just fine if the user has an whole number in strFloat, but once they enter a decimal number it blowsup, only returning zero into buf. Thanks for all the help folks. :) I honestly didn't think this would be that difficult.:-O -Eric

                          T Offline
                          T Offline
                          Tim Smith
                          wrote on last edited by
                          #12

                          The problem is you are working with UNICODE strings and trying to use the ANSI conversion routine. That is just silly. As people have already said, use the UNICODE conversion routine "wtof". float fResult = wtof (str); That will work just fine. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                          V 1 Reply Last reply
                          0
                          • T Tim Smith

                            The problem is you are working with UNICODE strings and trying to use the ANSI conversion routine. That is just silly. As people have already said, use the UNICODE conversion routine "wtof". float fResult = wtof (str); That will work just fine. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                            V Offline
                            V Offline
                            VanHlebar
                            wrote on last edited by
                            #13

                            Tim Smith wrote: The problem is you are working with UNICODE strings and trying to use the ANSI conversion routine. That is just silly. As people have already said, use the UNICODE conversion routine "wtof". float fResult = wtof (str); That will work just fine. Tim Smith Tim, Thanks for the suggestion. The wtof function works fine, but it truncates the value to a whole number. I lose the precision after the decimal if I use this function. I need a value going out only to the tenths, but I do need to keep the one decimal place. -Eric

                            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