Convert a number to a string with specified length in C++
or using the stringstreams: #include <sstream> #include <iomanip> std::stringstream ss; ss << std::setw(10) << std::setfill(‘0’) << i; std::string s = ss.str(); I compiled the information I found on arachnoid.com because I like the type-safe way of iostreams more. Besides, you can equally use this code on any other output stream.