Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Round up int  RSS feed

 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int x = Math.round((729/10));
x = 72
OR
int x = (int)Math.ceil((729/10));
x = 72

Whatever the number happens to be rounded, I always wanted rounded up, in this case 73. If it's 701/10, round up to 71.
Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer arithmetic always rounds down (towards zero). If you look at the documentation, those methods that you use take floating point. You can't round() or ceil() a number, when it is always a whole number.

If you want to control rounding, I would suggest that you convert to floating point before you do the operation. This way, you'll have a floating point result that can be rounded -- either up or down.

Henry
 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking you mean this?
float x = 829/10;
int y = Math.round(x);
Which still gives me 82
??

Thanks

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float x = 829f/10f;
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other options...

int x = Math.round((729.0/10.0));

OR

int x = (int)Math.ceil((729.0/10.0));


Henry
 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you very much Mr. Henry Wong.
Hooray!!!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use float, but double.

Try . . . round( 1.0 * i / j) . . .
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use "1.0 *" just to get a double. Although unlikely that may already cause rounding issues. Just cast to double: "(double)i / j". Since the first operand turns from int to double the entire calculation and result become double.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Don't use "1.0 *" just to get a double. Although unlikely that may already cause rounding issues.

Hmm, I don't agree with this. Based on JLS 4.2.3, any integer value with magnitude less than 2^53 has an exact double representation. There is no rounding error here. That applies to any int, but not any long - some longs are outside this range.

However even for values outside that range, the IEEE 754 round-to-nearest rule guarantees that adding extra zeroes after the decimal point cannot change what double value you end up rounding to. So these are guaranteed to all have the same double value, even though that value isn't really an exact representation of the original long value:

It's true that using doubles can introduce roundoff error. But in a case like this where using a double is necessary, I can't see anything wrong with achieving that by appending ".0" to the number you wish to make a double. It works exactly the same way as far as I can tell. Is there something I'm overlooking? Do you have a counterexample?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edit Misread, scratch that

How about
 
Dave Lerner
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This works pretty nicely...
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That uses a different rounding mode, namely round up. Maybe the original poster was looking for round half-up. You can get that with different placement of the () and 0.5 instead of 1.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is a zombie and all, but I'll just point out that an easy way to round up A / B to the next highest B whenever there's a remainder, without using FP, is simply (A + B - 1) / B, minding, of course, the wraparound if A+B-1 > Integer.MAX_VALUE.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!