0

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

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by vijayan121
0
#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' ;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.