Library contents

Scala java.lang.Math.ceilDiv

Integer ceiling division rounds the quotient up without converting to floating point.

A simple example

java.lang.Math.ceilDiv

  1. Start with 7 divided by 3
  2. Do this Round the quotient up
  3. Result 3
Two groups of three cover six. A third group covers the remaining one.
Lesson.scala
object Lesson extends App {
    println(java.lang.Math.ceilDiv(7, 3))
}
Output
3

Syntax

Scala syntax
java.lang.Math.ceilDiv(orders, capacity)
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 Lesson

Count crates for an order

A dispatch team packs an order into crates of a fixed capacity. Work out how the order fits the packing rule when the quantity does not divide evenly.

From the programval orders = 17
Result5
Lesson.scala

object Lesson {
    def main(args: Array[String]): Unit = {
        val orders = 17
        val capacity = 4
        println(java.lang.Math.ceilDiv(orders, capacity))
    }
}
Output
5

Four full crates carry sixteen items and a fifth holds the remaining item.

Advertisements

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.

Given31 badges · 8 per tray
Worked result4 trays

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.

Given48 cards · 12 per carton
Worked result4 cartons

An exact multiple leaves no partial carton.

What to watch for

The divisor must be nonzero. For packing work a negative capacity is meaningless.

Documentation for java.lang.Math.ceilDiv