Hi

I was just wondering if there was a way to convert a value of double to a string? And if there is what is the command?

Thank You in advance

#include <sstream>
#include <string>
#include <iostream>

template< typename T > inline std::string to_string( const T& v )
{
  std::ostringstream stm ;
  return (stm << v) ? stm.str() : "error" ;
}

int main()
{
  double value = 1.2345 ;
  std::string str_value = to_string(value) ;

  // c++0x
  // std::string str_value = lexical_cast<std::string>(value) ; 

  std::cout << value << '\t' << str_value << '\n' ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.