Scala java.lang.Math.ceilDivExact
Divide signed integers and round toward positive infinity with overflow checks.
A simple example
java.lang.Math.ceilDivExact
- Start with 7 divided by 3
- Do this Round the quotient up
- Result 3
object Lesson extends App {
println(java.lang.Math.ceilDivExact(7, 3))
}
3
Syntax
java.lang.Math.ceilDivExact(guests, seats)
Run these examples in Scala
Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.
scalac Lesson.scala
scala LessonBook enough tables while checking division
A booking clerk knows how many guests will arrive and how many fit at one table. Test a party that leaves a partial table so the final guest still has a seat.
object Lesson {
def main(args: Array[String]): Unit = {
val guests = 17
val seats = 4
println(java.lang.Math.ceilDivExact(guests, seats))
}
}
5
Four tables seat only sixteen guests. The seventeenth guest needs a fifth table.
Pack badges for two classrooms
A teacher has 31 badges and uses trays that hold eight each. All badges must travel, including those in a partially filled tray.
Three trays carry 24 badges. The remaining seven need the fourth tray.
Send a full print batch
A print shop has 48 cards and cartons that hold twelve each. The packer checks whether another carton is needed after the fourth.
An exact multiple leaves no partial carton.
What to watch for
Validate a positive capacity first. Zero capacity raises ArithmeticException.