[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppDoubleToStr
(function taken from from Marshall Cline's C++ FAQ Lite document http://www.parashift.com/c++-faq-lite/)
You could also use the Boost C++ Library's lexical_cast:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppDoubleToStr
(C++) Converting a double to a std::string
When not using the Boost C++ Library
In analogy with the standard 'atof' function, doing the other way around:(function taken from from Marshall Cline's C++ FAQ Lite document http://www.parashift.com/c++-faq-lite/)
std::string ftoa(const double& x) { std::ostringstream o; if (!(o << x)) return "ERROR"; return o.str(); } int main() { assert(ftoa("69.69")==69.69); }
- include <sstream>
- include <cassert>
You could also use the Boost C++ Library's lexical_cast:
int main() { const double d = 123.456; const std::string s = boost::lexical_cast<std::string>(d); assert(s == "123.456"); }
- include <cassert>
- include <string>
- include <boost/lexical_cast.hpp>
Other code snippets
Topic links
- ::, scope operator
- #include
- assert
- char
- const
- double
- #include
- int
- main
- return
- ?sstream
- std
- std::string
- string
- scope operator, ::
- return
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
