This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Formatting as a percent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Formatting as a percent" Watch "Formatting as a percent" New topic
Author

Formatting as a percent

Zach Spoelstra
Greenhorn

Joined: Feb 19, 2002
Posts: 2
Howdy...Nice board ya got here
OK. I am working on a program for class and I cant get my double variable to properly format as a percent. We were told to use the NumberFormat class and the getPercentInstance method. This is what I am using now and it keeps truncating the decimal portion of my number.
Here is my code:

I am guessing there is just something simple that I am missing here
[ February 19, 2002: Message edited by: Zach Spoelstra ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Do you have access to the API documentation? (Javadocs)? If so, you could just look up the NumberFormat class and see the API methods there and probably solve this problem yourself.
I just looked it up really quickly and there are two methods that might be useful:
NumberFormat.setMaximumFractionDigits(int);
NumberFormat.setMinimumFractionDigits(int);

Hope this helps!


Rob
SCJP 1.4
Zach Spoelstra
Greenhorn

Joined: Feb 19, 2002
Posts: 2
Actually I have looked extensively at the API and I couldn't for the life of me figure out how to use some of those. I'll give them a shot and report back.
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Formatting as a percent