This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Round  RSS feed

 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to round a double to a 2 decimal place?
I am using jdk1.3
Thanks!
 
Pablo Gomes Ludermir
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!