It is particularly useful for making listings line up by assigning fixed widths to your fields or for displaying in hex. G format gives you an ability like the G format in FØRTRAN, which will display in F format or E format as appropriate.
The java.text.DecimalFormat and java.text.NumberFormat classes in JDK 1.1+ give you similar ability a bit more verbosely. Java 1.0.2 has no equivalent to the C printf or sprintf function for formatting numbers for display. You have to roll your own. One such class is available from San Diego State University called sdsu.FormatString. Acme also has one. Eliote Rusty Harold did one. Gary Cornell and Cay Horstmann’s book Core Java 2 includes one.Most people don’t bother with printf. They use the + concatenation operator instead of printf.
// in idiomatic Java you would say: System.out.println( "x:" + x + " y:" + y ); // instead of C-style System.out.printf( "x: %d y: %d", x, y );
![]() |
recommend book⇒Core Java 2, Volume I: Fundamentals (6th Edition) | ||||||||||||||||||
| paperback | kindle | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ISBN13: | 978-0-13-047177-2 | ||||||||||||||||||
| ISBN10: | 0-13-047177-1 | B000OZ0N44 | |||||||||||||||||
| publisher: | Prentice Hall | ||||||||||||||||||
| published: | 2002-08-22 | ||||||||||||||||||
| by: | Cay Horstmann and Gary Cornell | ||||||||||||||||||
| Covers JDK 1.4 such as regular expressions, New I/O, assertions, Preferences, Swing enhancements and logging. Aimed at someone who already knows C++. This is not a fluffy introductory book. | |||||||||||||||||||
| |||||||||||||||||||
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.16] | The information on this page is for non-military use only. | ||
| You are visitor number 30,698. | Military use includes use by defence contractors. | ||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/printf.html | J:\mindprod\jgloss\printf.html | ||