【问题标题】:prepending number with 0's [duplicate]在数字前加 0 [重复]
【发布时间】:2010-11-07 08:56:18
【问题描述】:

可能重复:
Print leading zeros with C++ output operator (printf equivalent)?

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

我希望输出为00016
setw() 前面带有空格。 setw()前面不是可配置的字符吗?

我的最终目标是在 4 个位置打印一个 2 字节的十六进制数字。像这样的:

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

我希望得到这个输出:00A7

【问题讨论】:

  • @paxdiablo:是的,你是对的,它是重复的。抱歉,投票结束

标签: c++ formatting iostream setw iomanip


【解决方案1】:

你还需要setfill('0')。

【讨论】:

  • 谢谢!和??? 将是setw()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2023-03-26
  • 2015-10-14
  • 1970-01-01
  • 2020-05-30
相关资源
最近更新 更多