0

Hi,
Can someone please tell me,
Is there any builtin function to convert decimal value to a fixed format?
e.g. 6 to 0006
10 to 0010
123 to 0123
4987 to 4987

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Radical Edward
0

You can do this with streams and manipulators:

#include <iomanip>
#include <iostream>

int main()
{
  std::cout << std::setw(4) << std::setfill('0') << 6 << '\n';
}

A string stream can be used to write the result to a string instead of stdout:

#include <iomanip>
#include <iostream>
#include <sstream>

int main()
{
  std::ostringstream oss;

  oss << std::setw(4) << std::setfill('0') << 6;
  std::cout << oss.str() << '\n';
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.