Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting number values code example needed

 
Chad Eaves
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to figure out how to formate a number with two whole numbers before the decimal place and three decimal places. I have looked at the DecimalFormat class but I have not got the code to work. Does anyone have some example code for how to do formatting?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!
This:
DecimalFormat df = new DecimalFormat("00.000");
System.out.println(df.format(2.4300001)); // prints 02.430
would seem to do just what you are trying to do.
Two pointers:
The DecimalFormat documentation describes well how to use DecimalFormat.
The link to the search page to search this forum (and others at JavaRanch) is at the top right of this page. Lots of great information can be found using it - and DecimalFormat is a topic that comes up over, and over, and over...
Good Luck.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic