整数と浮動小数点数を書式化して表示
明解C言語 入門編 > 2. 演算と型 >
整数と浮動小数点数を書式化して表示
C
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("[%d]\n", 123);
printf("[%.4d]\n", 123);
printf("[%4d]\n", 123);
printf("[%04d]\n", 123);
printf("[%-4d]\n\n", 123);printf("[%d]\n", 12345);
printf("[%.3d]\n", 12345);
printf("[%3d]\n", 12345);
printf("[%03d]\n", 12345);
printf("[%-3d]\n\n", 12345);printf("[%f]\n", 123.45);
printf("[%.1f]\n", 123.45);
printf("[%4.1f]\n", 123.45);
printf("[%10.10f]\n\n", 123.45);return 0;
}
実行結果
R:\>lesson015\project1.exe
[123]
[0123]
[ 123]
[0123]
[123 ][12345]
[12345]
[12345]
[12345]
[12345][123.450000]
[123.5]
[123.5]
[123.4500000000]
C++
#include <iostream.h>
#include <iomanip.h>int main(int argc, char* argv[])
{cout << "[" << 123 << "]" <<endl;
cout << "[" << setfill('0') << setw(4) << 123 << "]" <<endl;
cout << "[" << setfill(' ') << setw(4) << 123 << "]" <<endl;
cout << "[" << setfill(' ') << setw(4) << left << 123 << "]" <<endl;cout << "[" << 12345 << "]" <<endl;
cout << "[" << setfill('0') << setw(3) << 12345 << "]" <<endl;
cout << "[" << setfill(' ') << setw(3) << 12345 << "]" <<endl;
cout << "[" << setfill(' ') << setw(3) << left << 12345 << "]" <<endl;cout << "[" << 123.45 << "]" << endl;
cout << "[" << setw(11) << setprecision(5) << right << 123.45 << "]" << endl;
cout << "[" << setw(11) << setprecision(1) << 123.45 << "]" << endl;
cout << "[" << setw(11) << fixed << setprecision(1) << 123.45 << "]" << endl;
cout << "[" << setw(11) << setprecision(5) << left << 123.45 << "]" << endl;
cout << "[" << setw(11) << setfill('0') << setprecision(5) << 123.45 << "]" << endl;return 0;
}
実行結果
T:\>lesson015\Project1.exe
[123]
[0123]
[ 123]
[123 ]
[12345]
[12345]
[12345]
[12345]
[123.45]
[ 123.45]
[ 1e+02]
[ 123.5]
[123.45000 ]
[123.4500000]
Delphi
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;
begin
write(format('[%d]'#13#10, [123]));
write(format('[%.4d]'#13#10, [123]));
write(format('[%4d]'#13#10, [123]));
write(format('[%04d]'#13#10, [123]));
write(format('[%-4d]'#13#10#13#10, [123]));write(format('[%d]'#13#10, [12345]));
write(format('[%.3d]'#13#10, [12345]));
write(format('[%3d]'#13#10, [12345]));
write(format('[%03d]'#13#10, [12345]));
write(format('[%-3d]'#13#10#13#10, [12345]));write(format('[%f]'#13#10, [123.45]));
write(format('[%.1f]'#13#10, [123.45]));
write(format('[%4.1f]'#13#10, [123.45]));
write(format('[%10.10f]'#13#10#13#10, [123.45]));
end.
実行結果
S:\>lesson015\project1.exe
[123]
[0123]
[ 123]
[ 123]
[123 ][12345]
[12345]
[12345]
[12345]
[12345][123.45]
[123.5]
[123.5]
[123.4500000000]
Perl
printf("[%d]\n", 123); printf("[%.4d]\n", 123); printf("[%4d]\n", 123); printf("[%04d]\n", 123); printf("[%-4d]\n\n", 123); printf("[%d]\n", 12345); printf("[%.3d]\n", 12345); printf("[%3d]\n", 12345); printf("[%03d]\n", 12345); printf("[%-3d]\n\n", 12345); printf("[%f]\n", 123.45); printf("[%.1f]\n", 123.45); printf("[%4.1f]\n", 123.45); printf("[%10.10f]\n\n", 123.45);
実行結果
L:\>perl lesson_02_015.pl
[123]
[0123]
[ 123]
[0123]
[123 ][12345]
[12345]
[12345]
[12345]
[12345][123.450000]
[123.5]
[123.5]
[123.4500000000]
Ruby
printf("[%d]\n", 123) printf("[%.4d]\n", 123) printf("[%4d]\n", 123) printf("[%04d]\n", 123) printf("[%-4d]\n\n", 123) printf("[%d]\n", 12345) printf("[%.3d]\n", 12345) printf("[%3d]\n", 12345) printf("[%03d]\n", 12345) printf("[%-3d]\n\n", 12345) printf("[%f]\n", 123.45) printf("[%.1f]\n", 123.45) printf("[%4.1f]\n", 123.45) printf("[%10.10f]\n\n", 123.45)
実行結果
L:\>ruby l:\lesson_02_015.rb
[123]
[0123]
[ 123]
[0123]
[123 ][12345]
[12345]
[12345]
[12345]
[12345][123.450000]
[123.5]
[123.5]
[123.4500000000]