printfのようなもの
戻る
import java.io.*;
import java.util.*;
/**
* $Id: printf.html,v 1.1 2009/06/22 16:12:20 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class PrintfTest {
static public void main( String[] args ) {
try {
String result = sprintf( "%04d", 105 );
System.out.println( result );
} catch ( Exception e ) {
e.printStackTrace();
}
for ( double d = 0;d < 100.0;d += 3.85 ) {
System.out.printf( "%8.2f\n", d );
}
}
static String sprintf( String format, Object obj ) throws Exception {
String value = null;
try {
// C言語のsprinfもどき
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter( baos );
pw.printf( format, obj );
pw.flush();
pw.close();
value = baos.toString( "UTF8" );
baos.close();
} catch ( Exception e ) {
throw e;
} finally {
return value;
}
}
}
■実行結果
0105
0.00
3.85
7.70
11.55
15.40
19.25
23.10
26.95
30.80
34.65
38.50
42.35
46.20
50.05
53.90
57.75
61.60
65.45
69.30
73.15
77.00
80.85
84.70
88.55
92.40
96.25
戻る