C++ examples for Data Type:Number Format
Use stream manipulators in <iomanip> to format your numeric data to string.
#include <iostream> #include <iomanip> #include <string> #include <sstream> using namespace std; int main() {// ww w.j a va2s. c om stringstream ss; ss << "There are " << 9 << " test cases."; cout << ss.str() << endl; // stringstream::str() returns a string with the contents ss.str(""); // Empty the string ss << showbase << hex << 16; // Show the base in hexadecimal cout << "ss = " << ss.str() << endl; ss.str("this is a test"); ss << 3.14; ss << setprecision(6) << 3.14285; cout << "ss = " << ss.str() << endl; }