Hello everyone, just a small question. In kotlin l...
# getting-started
a
Hello everyone, just a small question. In kotlin lets say I do, 
(10/3).toInt()
 it returns 3 but what I want is the upper value i.e. 4. How can I achieve it?
i
Perhaps do the
ceil
of
10/3
before
toInt()
?
a
Thanks @Igor Milakovic
👍 1
r
Keep in mind that you’re performing an integer division and you’ll always get an
int
from that, one of the operands should be a
double
or a
float
to get that desired behaviour.
e
If you want to round up integer division, then add denominator minus one to the nominator before division, e.g.
(10 + 3 - 1) / 3
1
mind blown 12
r
just make sure
nominator + denominator
doesn’t overflow 😂
d
you can avoid the overflow doing
1 + (nominator - 1) / denominator
, e.g.
1 + (10 - 1) / 3
👀 1
m
Maybe there should be a std function - `assertEquals(4, 10 divUp 3)`I have defined it as a util in my project. Example impl:
Copy code
infix fun Int.divUp(denominator: Int) = 1 + (this - 1) / denominator  // delegate handling of division by 0 to the / operator
might need special handling for nevative values though.