Author |
Message |
Delta
|
Posted: Thu Jul 01, 2004 1:43 pm Post subject: Rounding Decimal Numbers? |
|
|
How do you round to decimal places?
for example how do I round
8.49999999
to
8.5
Please explain. Thank you. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Paul
|
Posted: Thu Jul 01, 2004 2:16 pm Post subject: (No subject) |
|
|
isn't it Math.round?
like
code: |
System.out.println(x + " is approximately " + Math.round(x));
|
|
|
|
|
|
|
Tony
|
Posted: Thu Jul 01, 2004 2:53 pm Post subject: (No subject) |
|
|
no, Math.round would round to an integer. What you do is you multiply it by 10^number of decimal places, round, and then divide back.
such as
8.499 *10^1
84.99 ~ round
85 /10^1
8.5 |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Delta
|
Posted: Thu Jul 01, 2004 3:28 pm Post subject: (No subject) |
|
|
That's the thing tony... I asked how to round... |
|
|
|
|
|
Tony
|
|
|
|
|
wtd
|
Posted: Thu Jul 01, 2004 4:13 pm Post subject: (No subject) |
|
|
Do you just need this for formatting text? |
|
|
|
|
|
zylum
|
Posted: Fri Jul 02, 2004 1:10 pm Post subject: (No subject) |
|
|
tony wrote: no, Math.round would round to an integer. What you do is you multiply it by 10^number of decimal places, round, and then divide back.
such as
8.499 *10^1
84.99 ~ round
85 /10^1
8.5
i think tony's way is the only way to do it unless there is some weird function i havent heard of...
code: |
double num = 8.4999;
num *= 10;
num = Math.round(num);
num /= 10;
// or just num = Math.round(num * 10) / 10;
System.out.println(num); // gets you 8.5
|
|
|
|
|
|
|
Dan
|
Posted: Fri Jul 02, 2004 2:12 pm Post subject: (No subject) |
|
|
Thats how i do it also. Alougth u whould think there whould be a method for it some where in one of thous java APIs. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
Sponsor Sponsor
|
|
|
LiquidDragon
|
Posted: Fri Jul 09, 2004 7:26 pm Post subject: (No subject) |
|
|
I always thought there was a round method. But i may be wrong |
|
|
|
|
|
zylum
|
Posted: Fri Jul 09, 2004 11:42 pm Post subject: (No subject) |
|
|
there is a round method but it rounds to the nearest one not the nearest tenth. |
|
|
|
|
|
Delta
|
Posted: Sat Jul 24, 2004 8:13 am Post subject: (No subject) |
|
|
Ok well I found some nice lil rounding methods... (I believe this was just for rounding so I could display the text)
code: | import java.text.*;
DecimalFormat decF = new DecimalFormat ("###.##");
System.out.println (decF (99999.999999)); |
also
code: | decF.setMinimumFractionDigits(2); |
would set the format to a minimum of two decimal places
code: | decF.setMaximumFractionDigits(2); |
would set the format to a maximum of two decimal places
The decimal format thing is only for text as far as I know (for outting it)...
but when it came to storing the value I usually ended up using 'long' instead of 'int' or 'double'... because long would actually round how I wanted it to... never the less... thanks for the help.... Have a nice day. |
|
|
|
|
|
|