You're describing the ceiling function rather than round. Rounding typically implies going toward the closer value, and "rounding up" means that you go up when the number to be rounded is exactly between the two nearest integers. Regardless, you can check out the java.util.Math class for methods that will assist you in creating your method.

You should also investigate the modulo operator (%). "x % y" evaluates to the integer remainder of divinding the integer x by the integer y.Actually, you can create your method without the use of java.util.Math (but it's good to know that class) and use only the built-in operators.

As we're here to help people learn, I don't want to just write it for you. Try a few solutions and post if you get stuck.

Mark Rem
Greenhorn

Joined: Dec 11, 2004
Posts: 13

posted

0

int roundUp( int ) { int x, y; x % 10 = y; if( y >= 1 && y < 9 ) { y = y + 10; }

Like David, I was thinking of ceiling but it looks like you're going with the 10s complement which will work, too.

There's different way to look at "rem >= 1 && rem < 10" in this case that will simplify the conditional expression a little bit. If your only choices are 0, 1, 2, ..., 9 then how would you write the condition so you don't have to use the "&&" operator? Hint: If it's a number from 1 to 9, then it's not what?.

The ceiling method would look at it this way: Given X where X is not a multiple of 10, what is the smallest multiple of 10 that is greater than X? Solution: Add 10 to X, then round down to the nearest 10.

Of course now you have to figure out how to round down. Rounding down is more straightforward though considering that in Java, integer division ignores the remainder. That is,

int x = 11 / 10; // x == 1 int x = 19 / 10; // x == 1 [ January 29, 2005: Message edited by: Junilu Lacar ]

Make sure you understand exactly how that works. Getting the answers to homework handed to you may seem like a great way to zip through your classes, but if your goal is to learn Java, depending on them will not get you there.

That's the reason many of us -- especially in the beginners' forum -- try to give hints rather than solutions (though it's hard to resist the temptation of a quick ego boost sometimes ). [ January 29, 2005: Message edited by: David Harkness ]

For some reason I have had this problem a number of times in the past. I really can't remember why. Maybe it came up in computing page numbers and starting row numbers when paging forward and backward through lists back in COBOL days. Anyhow, I wound up with (((n + 9) / 10) * 10). You can replace 9 and 10 with x-1 and x where you need to round up to the nearest x.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Originally posted by vinodkumar kt: check this roundup int (int x){ (snip)

You're cool man, thanks a lot!

I'm sorry, but NO, that's not cool. Despite the best efforts of some folks to try to help you learn by doing things yourself, you take the "easy" way out and grab the first bit of code thrown your way. Adding insult to injury, you go thank the guy who, IMHO, is not helping you at all (that is, if you're in any way serious about learning Java) :roll: