[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppStrToDouble

(C++) Converting a std::string to a double

Use the function 'atof', meaning 'ASCII TO Float'.

  1. include <string>
  2. include <cassert>
int main() { const std::string myString = "123.456"; const double myValue = atof(myString.c_str()); assert(myValue == 123.456); return 0; }


You could also use the Boost C++ Library's lexical_cast:

  1. include <string>
  2. include <cassert>
  3. include <boost/lexical_cast.hpp>
int main() { const std::string s = "123.456"; const double d = boost::lexical_cast<double>(s); assert(d == 123.456); return 0; }


Other code snippets

Converting a String to a double

Code links



last edited (November 4, 2006) by bilderbikkel, Number of views: 4377, Current Rev: 7 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.