Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stop the double type value up to 2 or 3 decimal

 
humair khan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double a=Math.random()*1000;
answer will com like this
567.2365874589;
but i want
567.23
how it could be possible
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Humair,
Look into java.text.DecimalFormat:

Regards,
Manfred.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a number and not a string, you can do it simply enough with the following code:
float unrounded = 567.2365874589f;
float rounded = Math.round(unrounded * 100) / 100.0f;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic