Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Round up int

M Wilson
Ranch Hand
Posts: 41
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
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
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
float x = 829f/10f;

Henry Wong
author
Sheriff
Posts: 23295
125
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

Thank you very much Mr. Henry Wong.
Hooray!!!

Campbell Ritchie
Marshal
Posts: 56546
172
Don't use float, but double.

Try . . . round( 1.0 * i / j) . . .

Rob Spoor
Sheriff
Posts: 21135
87
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
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

Dave Lerner
Greenhorn
Posts: 1

This works pretty nicely...

Campbell Ritchie
Marshal
Posts: 56546
172
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
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.