cout
先頭にゼロを付けて int を出力したいので、値1
は として出力され001
、値は として出力25
され025
ます。これどうやってするの?
7 に答える
以下で、
#include <iomanip>
#include <iostream>
int main()
{
std::cout << std::setfill('0') << std::setw(5) << 25;
}
出力は次のようになります
00025
setfill
デフォルトではスペース文字(' '
)に設定されています。setw
印刷するフィールドの幅を設定します。これで完了です。
一般的な出力ストリームのフォーマット方法を知りたい場合は、別の質問に対する回答を書きました。それが役立つことを願っています 。C++コンソール出力のフォーマットです。
これを達成する別の方法はprintf()
、C言語の古い関数を使用することです
これを次のように使用できます
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 または別の文字 (数値ではない) で埋めることができます。これよりもフォーマット指定子の
ようなものを書いた場合、空白は埋められません。これにより、パッドが設定され、空白が埋められます。%24d
2
24
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
cout.fill( '0' );
cout.width( 3 );
cout << value;
1 桁の値のインスタンスの埋め込み文字としてゼロを使用して日付と時刻を出力する別の例: 2017-06-04 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;
}
次の関数を使用します。私は好きではありません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); --> "001234"
// 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;
}
// Set up small 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;
}