printf

11月 29th, 2008 tocmoc

今までprocessingではC言語のprintfのように便利な出力関数がなくてprintlnめんどくせと思ってたが,JAVAの機能の方にあった.JS2E 5.0,JDK5で実装されてたようなので,ずいぶん前だ.2005年くらいか?

ただ,微妙にCとは書式が違うようなので,注意が必要.
ここが一覧で見れてベンリ.
http://www.ne.jp/asahi/hishidama/home/tech/java/formatter.html

Cと同じように使えるprintfすばらしい!

JAVA:
  1. int a=10;
  2. float b=12345.1234567;
  3. double c=12345.1234567;
  4. String str="abcde";
  5.  
  6. System.out.printf("%d %05d\n",a, a);
  7. System.out.printf("%f %.3f\n",b, b);
  8. System.out.printf("%f %.8f\n",c, c);
  9. System.out.printf("%s %S\n",str, str);
  10. System.out.printf("%d\n%,8f\n%10s\n",a,b,str);

出力は,

CODE:
  1. 10 00010
  2. 12345.123047 12345.123
  3. 12345.123047 12345.12304688
  4. abcde ABCDE
  5. 10
  6. 12,345.123047
  7.      abcde

でもあれ・・・?実数の精度がおかしいような・・・?processingのせい?