Rounding Numbers

Put your problem here if it does not fit any of the other categories.

Rounding Numbers

Postby ulatec » Tue Jun 08, 2010 1:45 am

Hello there,

I have been playing around with rounding numbers for quite some time trying to figure this one out. I have tried a few things such as BigDecimal, Math.round, and NumberFormat. But have not gotten any of them correct. I am looking for a way that I could do something as follows:
Code: Select all
1.0 = 1
1.1 = 2
1.5 = 2
2.0 = 2
2.2 = 3

In words, I would like to round the number up if it is bigger than the previous whole number, rather than up at "x.5"

What would be the easiest method of completing this task?
ulatec
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Mar 27, 2010 2:09 am

Top

Re: Rounding Numbers

Postby anarche » Tue Jun 08, 2010 2:07 am

around 12 lines of code, wrapped in a static method??
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Rounding Numbers

Postby ulatec » Tue Jun 08, 2010 2:14 am

anarche wrote:around 12 lines of code, wrapped in a static method??

Huh?
If I do put this in a static method, what operations would I use?
ulatec
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Mar 27, 2010 2:09 am

Re: Rounding Numbers

Postby nicholas.hauschild » Tue Jun 08, 2010 5:22 am

What anarche is suggesting is you could write your own method (a static one even!) to do this for you.

But...why do that when it is already done for you!!!

Math.ceil(double)
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Rounding Numbers

Postby ulatec » Tue Jun 08, 2010 5:32 am

Oh my gosh! Thank you so much, I somehow must have missed that. Thanks you very much Nicholas :)
ulatec
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Mar 27, 2010 2:09 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 11 guests