A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
stop the double type value up to 2 or 3 decimal
humair khan
Greenhorn
Joined: Aug 19, 2001
Posts: 2
posted
Aug 21, 2001 06:19:00
0
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
posted
Aug 21, 2001 06:25:00
0
Hi Humair,
Look into
java.text.DecimalFormat
:
DecimalFormat df = new DecimalFormat( "#.00" ); System.out.println( df.format( 567.236587 ) );
Regards,
Manfred.
David Weitzman
Ranch Hand
Joined: Jul 27, 2001
Posts: 1365
posted
Aug 21, 2001 07:40:00
0
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;
I agree. Here's the link:
http://aspose.com/file-tools
subject: stop the double type value up to 2 or 3 decimal
Similar Threads
Anyone finish this in a month!!!???
Why is "float f4 = 42e7" not considered a valid declaration of a float in Java?
WA #2 ..... word association
Values for weightx and weighty
Most addictive song ever.
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter