Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I print a formatted number?

 
Timothy Chen Allen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel a bit stupid about this, but I haven't figured out how to printed a formatted number to the console in Java. I was hoping for a "printf" or something like it. Here is a sample of what I'd like to do:

which prints out an accurate but useless table of numbers with 15 digits of accuracy. How can I make it print out only one or two digits?
Thanks in advance.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't it be neat if there was an interface called NumberFormat that would have implementations to format numbers?
http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html
Enjoy!
 
Timothy Chen Allen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas, you are the man. Not only will that do what I want, it will do it in my localization (Spain) which means numbers like "32,5". Thanks very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic