
-----------------------------------
newTOjava365
Wed Nov 24, 2004 9:26 pm

rounding in java
-----------------------------------
how do i round numbers to the nearest whole number (int)?

-----------------------------------
wtd
Wed Nov 24, 2004 9:35 pm


-----------------------------------
Ceiling:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#ceil(double)

Floor:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#floor(double)

-----------------------------------
zylum
Wed Nov 24, 2004 10:17 pm


-----------------------------------
yout forgot Math.round()  :lol:

-----------------------------------
bugzpodder
Thu Nov 25, 2004 11:43 am


-----------------------------------
or some casting would do:
(int)(x+0.5)

-----------------------------------
wtd
Thu Nov 25, 2004 1:38 pm


-----------------------------------
or some casting would do:
(int)(x+0.5)

The goal of programming should be expressivity.  You should be able to read a program and understand what the programmer meant to do.  

Math.round(x);

Does that vastly better than

(int)(x + 0.5)

The latter is correct, but not as good expressive as the first.

-----------------------------------
bugzpodder
Thu Nov 25, 2004 1:50 pm


-----------------------------------
i am guessing that integer casting is faster than Math.Round ;)

-----------------------------------
rizzix
Thu Nov 25, 2004 1:52 pm


-----------------------------------
but u can't simply assume that. they might have optimized Math.Round (since it's so commonly used) who knows :?:

-----------------------------------
wtd
Thu Nov 25, 2004 2:09 pm


-----------------------------------
i am guessing that integer casting is faster than Math.Round ;)

This attitude is called "premature optimization".  It kills projects.  

First make your program work, then make it fast.  Part of making that first step easier is making your code easier to read and understand.  If you use the cast, then you go back and read your code a few weeks later, you may not have any clue what that cast is about.  Now you have to read through the code and remember why you did that.

If you see "Math.round" (or floor or ceil), then you know instantly what's going on.

The best optimizations come out of elegantly written software.

-----------------------------------
bugzpodder
Thu Nov 25, 2004 2:39 pm


-----------------------------------
but u can't simply assume that. they might have optimized Math.Round (since it's so commonly used) who knows :?:
the casting thing is 2 operations.  but i think they do a lot of stuff in Math.Round, including checking for overflows, etc
look at the link i posted in the C# vs java thread in general programming.

wtd, of course that depends on the situation...  i've never being a professional developer, so i dont really regard these things as important.

-----------------------------------
wtd
Thu Nov 25, 2004 3:17 pm


-----------------------------------
If you ever hope to be a professional developer, start taking them seriously.

-----------------------------------
zylum
Thu Nov 25, 2004 3:41 pm


-----------------------------------
i think bugzpodder is in the programming contest mentality where everything needs to be as fast as possible or else it might time out  :wink:

-----------------------------------
bugzpodder
Sat Nov 27, 2004 3:35 pm


-----------------------------------
i think bugzpodder is in the programming contest mentality where everything needs to be as fast as possible or else it might time out  :wink:

right on ;)
