• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting doubles in a toString return

 
S Gregg
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I'm new here but think I'm going to become a regular visitor now that I started my Java class. I am having trouble with one of my assignments, I have a toString method that is supposed to print out information on a triangle. My problem is I am trying to format the numbers to 2 or 3 decimal places but having some difficulty. Here is my toString method


The call to the toString method looks like this


The length variables as well as the perimeter and area are the ones that are of type double and are the ones I'm trying to round off to 3dp.

Any help is greatly appreciated.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
visit this link
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul.p Kumar wrote:visit this link

That method is used internally already by the code S Gregg posted; String concatenation already converts all non-Strings using one of the overloaded String.valueOf methods.

S Gregg, check out String.format / java.util.Formatter or java.text.NumberFormat.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch

Agree with Rob; my preference would be to use Rob's first suggestion. That is very closely related to the Formatter methods; I think it simply creates a Formatter object and passes the arguments on to that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic