How do I convert a CString to a double in C++?

40,614

Solution 1

A CString can convert to an LPCTSTR, which is basically a const char* (const wchar_t* in Unicode builds).

Knowing this, you can use atof():

CString thestring("13.37");
double d = atof(thestring).

...or for Unicode builds, _wtof():

CString thestring(L"13.37");
double d = _wtof(thestring).

...or to support both Unicode and non-Unicode builds...

CString thestring(_T("13.37"));
double d = _tstof(thestring).

(_tstof() is a macro that expands to either atof() or _wtof() based on whether or not _UNICODE is defined)

Solution 2

You can convert anything to anything using a std::stringstream. The only requirement is that the operators >> and << be implemented. Stringstreams can be found in the <sstream> header file.

std::stringstream converter;
converter << myString;
converter >> myDouble;

Solution 3

with the boost lexical_cast library, you do

#include <boost/lexical_cast.hpp>
using namespace boost;

...

double d = lexical_cast<double>(thestring);

Solution 4

strtod (or wcstod) will convert strings to a double-precision value.

(Requires <stdlib.h> or <wchar.h>)

Share:
40,614
Steve Duitsman
Author by

Steve Duitsman

Software Developer with experience in: C# C++ ASP.NET Ajax ASP.NET MVC MFC I'm also passionate about: Software craftsmanship Usability/User experience Scrum Software engineering practices #SOreadytohelp

Updated on July 24, 2020

Comments

  • Steve Duitsman
    Steve Duitsman almost 4 years

    How do I convert a CString to a double in C++?

    Unicode support would be nice also.

    Thanks!

  • Steve Duitsman
    Steve Duitsman almost 15 years
    This link shows you "wcstod" which is what I used to support unicode. msdn.microsoft.com/en-us/library/kxsfc1ab(VS.80).aspx
  • Dave Hillier
    Dave Hillier almost 15 years
    Consider some more context to the page you're adding
  • Pete
    Pete almost 15 years
    This works, but IMO MighMoS's suggestion of std::stringstream is a bit cleaner.
  • Shog9
    Shog9 almost 15 years
    Usage examples wouldn't go amiss either... See the accepted answer.
  • Rolf Kristensen
    Rolf Kristensen almost 12 years
    _wcstod_l / _tcstod_l allows one to specify locale, so one can handle localized format (decimal delimiter as comma or dot etc.)
  • Philm
    Philm about 10 years
    This sounded good to me. Unfortunately it didn't work for the questioned scenario here with the Microsoft (MFC) CString type. I got this compiled, but the converted double number seemed to be random, at least it had no relation to the string. I hope, it works good for other types which implement the << and >> Operators better. I am no expert yet in using the std strings in C++, but to me a converter which results in wrong numbers without throwing an error or exception is very dangerous.
  • Philm
    Philm about 10 years
    Remark: The other Suggestion here, _tstof() worked fine for converting a CString to a double for me.
  • IInspectable
    IInspectable about 7 years
    @Philm: This answer can be used with MFC's CString objects just fine. You don't need an operator<<() either. That's what the constructor is for: std::wstringstream converter(myString);. Note that this requires a CStringW (which, in the majority of cases, is the correct type anyway). However, there's still a silent killer in there. It seems to be clear, elegant. And wrong. It uses the current thread locale for the conversion. This may or may not be what you want. Better to use a solution that allows you to explicitly specify the locale to use for the conversion.