
-----------------------------------
Delta
Thu Jul 01, 2004 1:43 pm

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.

-----------------------------------
Paul
Thu Jul 01, 2004 2:16 pm


-----------------------------------
isn't it Math.round?
like

System.out.println(x + " is approximately " + Math.round(x));     


-----------------------------------
Tony
Thu Jul 01, 2004 2:53 pm


-----------------------------------
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

-----------------------------------
Delta
Thu Jul 01, 2004 3:28 pm


-----------------------------------
That's the thing tony... I asked how to round...

-----------------------------------
Tony
Thu Jul 01, 2004 3:30 pm


-----------------------------------
i donno... you can typecast it to an (int) :lol:

-----------------------------------
wtd
Thu Jul 01, 2004 4:13 pm


-----------------------------------
Do you just need this for formatting text?

-----------------------------------
zylum
Fri Jul 02, 2004 1:10 pm


-----------------------------------
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...


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
Fri Jul 02, 2004 2:12 pm


-----------------------------------
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.

-----------------------------------
LiquidDragon
Fri Jul 09, 2004 7:26 pm


-----------------------------------
I always thought there was a round method. But i may be wrong  :roll:

-----------------------------------
zylum
Fri Jul 09, 2004 11:42 pm


-----------------------------------
there is a round method but it rounds to the nearest one not the nearest tenth.

-----------------------------------
Delta
Sat Jul 24, 2004 8:13 am


-----------------------------------
Ok well I found some nice lil rounding methods... (I believe this was just for rounding so I could display the text)

import java.text.*;

DecimalFormat decF = new DecimalFormat ("###.##");
System.out.println (decF (99999.999999));

also

decF.setMinimumFractionDigits(2);
would set the format to a minimum of two decimal places

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.
