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 Round 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 "Round" Watch "Round" New topic
Author

Round

Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
how to round a double to a 2 decimal place?
I am using jdk1.3
Thanks!
Pablo Gomes Ludermir
Greenhorn

Joined: May 29, 2003
Posts: 12
You can combine those methods in java.lang.Math class:
double ceil(double)
double floor(double)
int round(float)
long round(double)
[]s
Pablo
SCJP14
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

You can use a BigDecimal also like this:

Then you can turn this into a function that you call with any number and a decimal place that you want returned: getRoundedDouble( 123.3256 , 2 ) would return 123.33
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Round