This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting display

 
Richard Mendoza
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have displayed a value in my textfield of type double. I want to format the display into two decimal places. What method should I use?
Richard
 
Ajay Singhal
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can use NumberFormat class to get your results.
Ajay
 
Tom Wallace
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specifically, I'd try NumberFormat's setMaximumFractionDigits method:
double d = .25120;

NumberFormat f = NumberFormat.getInstance();

f.setMaximumFractionDigits(2);

System.out.println(f.format(d));
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic