I thought you might enjoy this recent post by Cay Horstmann to the
Advanced Placement in Computer Science list.

John

-------------------------------------------------------
John W. McCormick                [log in to unmask]
Computer Science Department      [log in to unmask]
University of Northern Iowa      voice  (319) 273-2618
Cedar Falls, IA  50614-0507      fax    (319) 273-7123


---------- Forwarded message ----------

How do you print a floating-point number with field width 10 and two digits
after the decimal point, using only the standard library?

C
   printf("%10.2f", x);


C++
   cout << setw(10) << setprecision(2) << showpoint << x;


Java
   java.text.NumberFormat formatter
      = java.text.NumberFormat.getNumberInstance();
   formatter.setMinimumFractionDigits(2);
   formatter.setMaximumFractionDigits(2);
   s = formatter.format(x);
   for (int i = s.length(); i < 10; i++)
      System.out.print(' ');
   System.out.print(s);


Cay
Cay S. Horstmann

mailto:[log in to unmask]    Phone: 408/865-1895    10411 Flora Vista Ave.
http://www.horstmann.com    FAX:   408/865-0369    Cupertino, CA 95014