Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

C's printf()  RSS feed

 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there such a fruit in Java?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly.
You can call System.out.println() and pass it a single object reference or primitive value. It's overloaded so it will accept any of the Java primitives, or an object reference, or a String (which is just a subclass of Object).
Java separates formatting from value, so you don't get the same kind of formatting functionality with println() that you do with printf(). You can, however, use various NumberFormat classes to format a number anyway you want.
[ June 05, 2002: Message edited by: Rob Ross ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just discovered this thread by accident; what the heck, better late than never.
In general I agree that NumberFormat is the way to go (especially
DecimalFormat). The main thing which seems to be lacking is the ability to pad with spaces (or other chars), particularly on the left side. Admittedly this facility is only useful when using monowidth fonts, which cannot be assumed in this day and age. Nonetheless, you may want to check out Cay Horstmann's Format to see echoes of the familiar C printf() function. Personally, if using text files rather than GUI components, I prefer to use DecimalFormat plus a custom padLeft() function (which I could show you if we still had a good search function at JavaRanch) ). Still, Horstmann's Format class is worth a look for ideas at least.
[ July 16, 2002: Message edited by: Jim Yingst ]
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found these two links:
http://www.braju.com/
http://sharkysoft.com/software/java/printf/
/Rene
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and another link:
http://developer.java.sun.com/developer/technicalArticles/Programming/sprintf/
PS. I know it is 'sprintf' but it is almost the same
/Rene
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!