What is the floor() method in Scala?

The floor() method in Scala gets an integer that is less than or equal to a given number by rounding down the given number.

Syntax

The floor() method can be declared as shown in the code snippet below:

(num).floor
  • num: The number whose floor value is required.

Return value

The floor() method returns the floor value of a number by rounding down the number.

Example

The code snippet below demonstrates the use of the floor() method:

object main {
def main(args: Array[String]) = {
val n1 = -2.5;
println("(-2.5).floor: " + (n1).floor);
val n2 = -2.2;
println("(-2.2).floor: " + (n2).floor);
val n3 = 0;
println("(0).floor: " + (n3).floor);
val n4 = 3.9;
println("(3.9).floor: " + (n4).floor);
val n5 = 3.1;
println("(3.1).floor: " + (n5).floor);
}
}

Free Resources