Sarah Shay

Greenhorn

Posts: 14

posted 12 years ago

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

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

M Beck

Ranch Hand

Posts: 323

posted 12 years ago

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.

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.

Layne Lund

Ranch Hand

Posts: 3061

posted 12 years ago

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

Layne

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 12 years ago

To round up integer division add divisor-1 before dividing.

127 + 9 / 10 = 13

127 + 49 / 50 = 3

127 + 9 / 10 = 13

127 + 49 / 50 = 3

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi