Connecting Tech Pros Worldwide Help | Site Map

Conversion from int and float to string / c++

  #1  
Old July 23rd, 2005, 05:51 AM
Kamil Grymuza
Guest
 
Posts: n/a
Hello
How to convert int and float values to strings? I mean std::string not
char*.

Thanks
Kamil Grymuza
  #2  
Old July 23rd, 2005, 05:51 AM
Mike Wahler
Guest
 
Posts: n/a

re: Conversion from int and float to string / c++



"Kamil Grymuza" <grymuz@wp.pl> wrote in message
news:d74r1j$pg0$1@nemesis.news.tpi.pl...[color=blue]
> Hello
> How to convert int and float values to strings? I mean std::string not
> char*.[/color]

#include <iostream>
#include <sstream>
#include <stream>

template <typename T>
std::string to_string(const T& value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}

int main()
{
std::string s1(to_string(42));
std::string s2(to_string(3.14f));

std::cout << s1 << '\n'
<< s2 << '\n';

return 0;
}

-Mike


  #3  
Old July 23rd, 2005, 05:51 AM
Victor Bazarov
Guest
 
Posts: n/a

re: Conversion from int and float to string / c++


Kamil Grymuza wrote:[color=blue]
> How to convert int and float values to strings? I mean std::string not
> char*.[/color]

See the FAQ or the archives. Alternatively, read about ostringstream
class.

V
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Float-to-string conversion that raises exception instead of returning0.0 like atof Trond Valen answers 20 December 7th, 2005 10:15 AM
sp_prepexec and compatiblity mode 7: float variables comparing to string field fails Peter Scheurer answers 1 July 20th, 2005 05:05 AM
[BUG] IMO, but no opinions? Uncle Tim? was: int(float(sys.maxint)) buglet ? Bengt Richter answers 6 July 18th, 2005 07:29 PM
int('2.1') does not work while int(float('2.1')) does Vineet Jain answers 9 July 18th, 2005 11:15 AM