Scala math.floor
Scala math.floor returns the greatest integer-valued Double less than or equal to its argument.
A simple example
math.floor
- Start with 2.8
- Do this Round downward to a whole number
- Result 2
object Lesson extends App {
println(math.floor(2.8))
}
2.0
Syntax
math.floor(value)
Run this example in Scala
Save the program as Lesson.scala. Use Scala 2.13.
scalac Lesson.scala
scala Lesson
Assign negative readings to intervals
A measurement below zero falls between two numbered intervals. Work through how negative values are assigned so the interval on the lower side of zero is labeled consistently with the positive side.
object Lesson {
def main(args: Array[String]): Unit = {
for (reading <- List(-0.2, 0.0, 1.0)) {
println(math.floor(reading).toString + " " + reading.toInt)
}
}
}
-1.0 0 0.0 0 1.0 1
A negative fraction falls in the interval beginning at −1. Converting it directly to Int truncates toward zero, producing a different result for negative nonintegers.
Scala documentation for math.floor
Cut complete straps from a roll
The costume room has 8.5 metres of ribbon left before a performance. Each bag needs one unbroken 1.2-metre strap. The volunteer needs the number of bags she can finish from this roll.
object Lesson {
def main(args: Array[String]): Unit = {
val metres = 8.5
val perStrap = 1.2
val straps = math.floor(metres / perStrap).toInt
println((straps).toString() + " straps")
}
}
7 straps
Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.
Make complete paint sets
A studio has 29 paint tubes and puts six in each set. It lists only sets that can be handed out complete.
Four sets use 24 tubes. Five tubes stay on the shelf.