• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Math issue

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Im trying to divide 2 numbers lets say 127 by 50. When I do I get a value of 3 returned. However I want 4 to be returned, asin I want the remainder to be recognised. How can I do this?

Thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. 127 / 50 is 2, not 3 or 4, with a remainder of 27, so I'm not exactly sure what you're after here.

But if you divide two integers, you get the quotient as an integer, ignoring the remainder, and you can use the "%" operator to get just the remainder -- i.e.,

127 / 50 --> 2
127 % 50 --> 27
 
Sarah Shay
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes its 2.5 roughly. But I want to round this up to 3 so I dont lose the decimal at the end
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could cast the values to doubles, so as to do a floating-point division, then round up, then after that convert back to integers if needed. the Math class has some rounding functions you might find useful.

or you could do an integer division the way you are, then do aseparate modulus operation to find the remainder, and add 1 to the result of the division if needed depending on what the modulus was. it's your choice which of the methods is more complicated and roundabout, i think.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need the decimal places, you should probably use floats or doubles to start with instead of just ints. Then if you need to, you can round these off from there.

Layne
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To round up integer division add divisor-1 before dividing.

127 + 9 / 10 = 13

127 + 49 / 50 = 3
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic