Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rounding up function

 
Mark Rem
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I go about creating an algorithm that would round up a positive integer by 10. For example, 1 -> 10; 11 -> 20; 91 -> 100.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int roundUp( int )
{
int x, y;
x % 10 = y;
if( y >= 1 && y < 9 )
{
y = y + 10;
}

return y;
}
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not quite right. y would end up being either 0, 9 or a number between 11 and 18. I think.

maybe
[code]
x%10 = y;
x -= y;
that should drop the last digit from x. I think you can figure out when you need to add 10 and where to add it.
 
Mark Rem
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this algorithm?

int roundUp( int )
{
int input, rem, x;

input % 10 = rem;
if( rem >= 1 && < 10 )
{
x = ( 10 - rem ) + input;
}

return ;
}
 
Junilu Lacar
Bartender
Posts: 7595
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
vinod kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this
roundup int (int x){

int y;
if(x%10 != 0){
y=((x/10)+1)*10;
}else{
y=x;
}
return y;
}
 
Mark Rem
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vinodkumar kt:
check this
roundup int (int x){

int y;
if(x%10 != 0){
y=((x/10)+1)*10;
}else{
y=x;
}
return y;
}




You're cool man, thanks a lot!
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Bartender
Posts: 7595
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Rem:

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:
 
Marcus Laubli
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright Mark,

I'm missing something (at least I have a reason :roll: ).

I don't get it. Can you explain what's happening in this code? I'd really like to know.

Mark's the only one allowed to anser me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic