开发者

prepending number with 0's [duplicate]

This question already has answers here: 开发者_运维百科 Closed 12 years ago.

Possible Duplicate:

Print leading zeros with C++ output operator (printf equivalent)?

#include <iostream>
#include <iomanip>
int main()
{
   int n = 16;
   std::cout << ???(5) << n << std::endl;
}

I want the output to be 00016

setw() prepends with spaces. Isn't it configurable what characters to prepend with setw()?

My eventual goal is to print a hex 2-byte number in 4 positions. Something like this:

#include <iostream>
#include <iomanip>
int main()
{
    unsigned short n = 0xA7;
    std::cout << std::hex << ???(4) << n << std::endl;
}

and I am expecting to get this output: 00A7


You also need setfill('0').

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜