Hope someone can tell me how to convert from double, long and int to a string of a specified length. I also need to to pad the string with zeros.
Hope someone can tell me how to convert from double, long and int to a string of a specified length. I also need to to pad the string with zeros.
An example converting an int value:
Output:Code:#include <sstream> #include <string> #include <iostream> #include <iomanip> using namespace std; int main() { ostringstream ostr; string str; int iVal = 157; // Convert int value into string value with '0' padding to 7 places ostr << setw(7) << setfill('0') << iVal; str = ostr.str(); // Output string cout << str << endl; return 0; }
Code:0000157
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens