| Author |
Round up int
|
M Wilson
Greenhorn
Joined: May 23, 2010
Posts: 26
|
|
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
Joined: Sep 28, 2004
Posts: 14606
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
M Wilson
Greenhorn
Joined: May 23, 2010
Posts: 26
|
|
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
Joined: Sep 28, 2004
Posts: 14606
|
|
|
float x = 829f/10f;
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 14606
|
|
Other options...
int x = Math.round((729.0/10.0));
OR
int x = (int)Math.ceil((729.0/10.0));
Henry
|
 |
M Wilson
Greenhorn
Joined: May 23, 2010
Posts: 26
|
|
Thank you very much Mr. Henry Wong.
Hooray!!!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26712
|
|
Don't use float, but double.
Try . . . round( 1.0 * i / j) . . .
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2127
|
|
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
Joined: May 03, 2008
Posts: 3212
|
|
edit Misread, scratch that
How about
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Dave Lerner
Greenhorn
Joined: Feb 03, 2012
Posts: 1
|
|
This works pretty nicely...
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26712
|
|
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
Joined: Jan 03, 2004
Posts: 3133
|
|
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.
|
 |
 |
|
|
subject: Round up int
|
|
|