• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Round up int

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float x = 829f/10f;
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you very much Mr. Henry Wong.
Hooray!!!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use float, but double.

Try . . . round( 1.0 * i / j) . . .
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
edit Misread, scratch that

How about
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This works pretty nicely...
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic