ForNext
Only Do What Only You Can Do
015. 整数と浮動小数点数を書式化して表示
VBScript
JScript
Perl
更新日 : 2010.10.18
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]
PHP
更新日 : 2010.11.03
<?php 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:\>php lesson_02_015.php [123] [123] [ 123] [0123] [123 ] [12345] [12345] [12345] [12345] [12345] [123.450000] [123.5] [123.5] [123.4500000000]
Python
更新日 : 2010.11.17
print "[%d]" % 123 print "[%.4d]" % 123 print "[%4d]" % 123 print "[%04d]" % 123 print "[%-4d]\n" % 123 print "[%d]" % 12345 print "[%.3d]" % 12345 print "[%3d]" % 12345 print "[%03d]" % 12345 print "[%-3d]\n" % 12345 print "[%f]" % 123.45 print "[%.1f]" % 123.45 print "[%4.1f]" % 123.45 print "[%10.10f]\n" % 123.45
N:\>python lesson_02_015.py [123] [0123] [ 123] [0123] [123 ] [12345] [12345] [12345] [12345] [12345] [123.450000] [123.5] [123.5] [123.4500000000]
Ruby
更新日 : 2010.11.01
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]
PowerShell
Scala
F#
C
更新日 : 2010.10.08
#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++
更新日 : 2010.10.13
#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]
C++Builder
VC++
C#
Java
更新日 : 2010.11.05
class Lesson015 { public static void main(String[] args) { System.out.printf("[%d]\n", 123); // System.out.printf("[%.4d]\n", 123); System.out.printf("[%4d]\n", 123); System.out.printf("[%04d]\n", 123); System.out.printf("[%-4d]\n\n", 123); System.out.printf("[%d]\n", 12345); // System.out.printf("[%.3d]\n", 12345); System.out.printf("[%3d]\n", 12345); System.out.printf("[%03d]\n", 12345); System.out.printf("[%-3d]\n\n", 12345); System.out.printf("[%f]\n", 123.45); System.out.printf("[%.1f]\n", 123.45); System.out.printf("[%4.1f]\n", 123.45); System.out.printf("[%10.10f]\n\n", 123.45); } }
L:\>java Lesson015 [123] [ 123] [0123] [123 ] [12345] [12345] [12345] [12345] [123.450000] [123.5] [123.5] [123.4500000000]
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2010.09.24
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]