Published on

`zfill` in C++

Authors

Here is a function in C++ that works like Python's zfill. It is convenient when you want to generate folder names or similar values with zero-padded serial numbers.

std::string zfill(const std::string& str, const int width)
{
  std::stringstream ss;
  ss << std::setw(width) << std::setfill('0') << str;
  return ss.str();
}