wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes stop the double type value up to 2 or  3 decimal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stop the double type value up to 2 or  3 decimal" Watch "stop the double type value up to 2 or  3 decimal" New topic
Author

stop the double type value up to 2 or 3 decimal

humair khan
Greenhorn

Joined: Aug 19, 2001
Posts: 2
double a=Math.random()*1000;
answer will com like this
567.2365874589;
but i want
567.23
how it could be possible


send me excercises about <BR>biggener level
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Humair,
Look into java.text.DecimalFormat:

Regards,
Manfred.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: stop the double type value up to 2 or 3 decimal
 
Similar Threads
Why is "float f4 = 42e7" not considered a valid declaration of a float in Java?
Values for weightx and weighty
WA #2 ..... word association
Anyone finish this in a month!!!???
Most addictive song ever.