回答
首先包括<iomanip>
,則:
cout << setfill('0') << setw(5) << 25;
output:
00025
setfill
被默認設置爲space ' '
。 setw
設置要打印的字段的寬度,就是這樣。
如果您有興趣瞭解如何在一般格式輸出流,我寫了另一個問題的答案,希望它是有用的: Formatting C++ Console Output.
但是..我怎麼能寫格式化輸出到字符串('char *或char []')而不是直接控制檯。其實我正在寫一個函數,返回格式化的字符串 – shashwat
@harsh使用std :: stringstream – cheshirekow
不要忘記恢復流格式後,你會得到一個討厭的驚喜後。 –
cout.fill('0');
cout.width(3);
cout << value;
另一種方式來實現這一目標是使用C語言舊printf()
功能
你可以使用這個像
int dd = 1, mm = 9, yy = 1;
printf("%02d - %02d - %04d", mm, dd, yy);
這將在控制檯上打印09 - 01 - 0001
。
您還可以使用其他功能sprintf()
來格式化輸出寫入字符串象下面這樣:
int dd = 1, mm = 9, yy = 1;
char s[25];
sprintf(s, "%02d - %02d - %04d", mm, dd, yy);
cout << s;
不要忘了包括stdio.h
頭文件在程序的這兩個功能
需要注意的事項:
您可以通過0或其他填充空格char(不是數字)。
如果您確實寫了類似%24d
格式的說明符,則不會在空格中填寫2
。這會將墊設置爲24
並填充空白區域。
我知道這是一箇舊的答案,但仍然應該指出,sprintf通常不應該太信任,因爲您無法指定它應該寫入的緩衝區的長度。使用snprintf往往更安全。使用流而不是* printf()也是更安全的類型,因爲編譯器有機會在編譯時檢查參數的類型; AraK公認的答案既是類型安全又是「標準」的C++,並且不依賴於中毒全局名稱空間的頭文件。 – Magnus
我會使用下面的函數。我不喜歡sprintf它不會做我想要的!
#define hexchar(x) ((((x)&0x0F)>9)?((x)+'A'-10):((x)+'0'))
typedef signed long long Int64;
// special printf for numbers only
// see formatting information below
// Print the number "n" in the given "base"
// using exactly "numDigits"
// print +/- if signed flag "isSigned" is TRUE
// use the character specified in "padchar" to pad extra characters
//
// Examples:
// sprintfNum(pszBuffer, 6, 10, 6, TRUE, ' ', 1234); --> " +1234"
// sprintfNum(pszBuffer, 6, 10, 6, FALSE, '0', 1234); --> "0"
// sprintfNum(pszBuffer, 6, 16, 6, FALSE, '.', 0x5AA5); --> "..5AA5"
void sprintfNum(char *pszBuffer, int size, char base, char numDigits, char isSigned, char padchar, Int64 n)
{
char *ptr = pszBuffer;
if (!pszBuffer)
{
return;
}
char *p, buf[32];
unsigned long long x;
unsigned char count;
// prepare negative number
if(isSigned && (n < 0))
{
x = -n;
}
else
{
x = n;
}
// setup little string buffer
count = (numDigits-1)-(isSigned?1:0);
p = buf + sizeof (buf);
*--p = '\0';
// force calculation of first digit
// (to prevent zero from not printing at all!!!)
*--p = (char)hexchar(x%base);
x = x/base;
// calculate remaining digits
while(count--)
{
if(x != 0)
{
// calculate next digit
*--p = (char)hexchar(x%base);
x /= base;
}
else
{
// no more digits left, pad out to desired length
*--p = padchar;
}
}
// apply signed notation if requested
if(isSigned)
{
if(n < 0)
{
*--p = '-';
}
else if(n > 0)
{
*--p = '+';
}
else
{
*--p = ' ';
}
}
// print the string right-justified
count = numDigits;
while(count--)
{
*ptr++ = *p++;
}
return;
}
cout.fill('*');
cout << -12345 << endl; // print default value with no field width
cout << setw(10) << -12345 << endl; // print default with field width
cout << setw(10) << left << -12345 << endl; // print left justified
cout << setw(10) << right << -12345 << endl; // print right justified
cout << setw(10) << internal << -12345 << endl; // print internally justified
這將產生輸出:
-12345
****-12345
-12345****
****-12345
-****12345
另一示例使用零作爲單一的數字值的實例的填充字符輸出的日期和時間:2017年6月4日18點13 :02
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
time_t t = time(0); // get time now
struct tm * now = localtime(&t);
cout.fill('0');
cout << (now->tm_year + 1900) << '-'
<< setw(2) << (now->tm_mon + 1) << '-'
<< setw(2) << now->tm_mday << ' '
<< setw(2) << now->tm_hour << ':'
<< setw(2) << now->tm_min << ':'
<< setw(2) << now->tm_sec
<< endl;
return 0;
}
- 1. 我如何墊在使用帶前導零的浮動的cout <<操作
- 2. JSTL填充int前導零
- 3. 使用<<運算符
- 4. <<運算符重寫爲cout int和double值
- 5. 使用零填充(前導零)將int轉換爲QString
- 6. 使用CIN >>和cout <<來填充類C++
- 7. 如何使用的cout << MyClass的
- 8. 試圖超載cout <<運算符但它不起作用
- 9. 如何用前導零填充整數?
- 10. 如何在Netezza SQL中使用前導零填充整數
- 11. 使用「<<」或「+」在使用「cout」時將字符串放在一起
- 12. 填充矢量<int> *
- 13. 在Java中使用零填充運算符右移(>>>)只使用「int」而不使用「byte」
- 14. INT填充零字符串
- 15. Java位運算符(零填充移位)不填充零
- 16. lazy_high_chart:如何使用'<'運算符發送evenet而不是'<'
- 17. 如何使用數組重載ostream <<運算符?
- 18. cout << cout'和'cout <<&cout'在C++中的區別?
- 19. 使用「<<」時「+」運算符的有效性
- 20. 錯誤重載運算符時<<
- 21. C++的std :: cout和<<運算符,優先
- 22. 超載<<運算符並將參數傳遞給std :: cout
- 23. 如何重載ostream運算符<<以使其在C++中使用log4cxx?
- 24. 如何使用列表<String>填充<form:select>?
- 25. 增量圖<string, int>使用++運算符
- 26. xcode中:<br> 「msgpack ::運算<:用於建築x86_64的[COUT <<]
- 27. Gradle:使用或不使用<<運算符定義任務時的差異
- 28. 模糊使用運算符<<在我的課堂內
- 29. 在Javascript中使用Ruby的<<連接運算符
- 30. 使用<<運算符指向字符的指針
可能出現[用C++輸出運算符打印前導零(printf等價物)?](http://stackoverflow.com/questions/530614/prin t-leading-with-c-output-operator-printf-equivalent) – atoMerz