Howto convert CString to float?
-
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 -
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 -
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
-
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
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
-
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
-
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
-
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
-
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
-
;);)Rage is right This should dp the work work. cheers Himanshu
-
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
-
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
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.
-
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.
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