int to string conversion

Jerry L Kreps JerryKreps at alltel.net
Sat Feb 12 04:19:25 GMT 2000


Martin Wiebusch wrote:
> 
> Hi,
> i was just wondering whether there is a function to convert integer
> values to strings in any of the libaries. I already tried itoa, Itoa and
> IntToStr. I also noticed there ist an Itoa function defined in the
> Integer.h header. But I don't really need an Integer class, just a
> little function to convert usual int variables into strings.

You can use I/O manipulators (page 259, Practical C++
programming)
#include <iostream.h>
#include <iomanip.h>
main()
#
{
  int number = 12;	// a number to output
  float	real = 12.34;	// a real number
	
  cout << "123456789012345678901234567890\n";	// output
ruler
  cout << number << "<-\n";
  cout << setw(5) << number << "<-\n";
  cout << setw(5) << setfill('*') << number << "<-\n";
  cout << setiosflags(ios::showpos|ios::left) << setw(5) <<
number << "<-\n";
  cout << real << "<-\n";
  cout << setprecision(1) << setiosflags(ios::fixed) << real
<< "<-\n";
  cout << setiosflags(ios::scientific) << real << <-\n";
  return(0);
}

The output of this program is:
123456789012345678901234567890
12<-
   12<-
***12<-
+12**<-
12.34<-
12.3<-
1e+01<-

hth,
JLK




More information about the KDevelop mailing list