Scala Tutorial Scala References

Scala - Math floorDiv() Method



The Scala Math floorDiv() method returns the largest Int value or largest Long value that is less than or equal to the algebraic quotient. The method first calculates the quotient by dividing the first argument with the second argument and finally returns the floor() of the quotient. The method can be overloaded and it can take Int and Long arguments. In special cases it returns the following:

  • If the second argument is zero, this method throws ArithmeticException.

Syntax

def floorDiv(x: Int, y: Int): Int = java.lang.Math.floorDiv(x, y)
def floorDiv(x: Long, y: Long): Long = java.lang.Math.floorDiv(x, y)

Parameters

x Specify the dividend.
y Specify the divisor.

Return Value

Returns the largest Int value or largest Long value that is less than or equal to the algebraic quotient.

Exception

Throws ArithmeticException, if the divisor y is zero.

Example:

In the example below, Math.floorDiv() method returns the floor() of algebraic quotient.

object MainObject {
  def main(args: Array[String]) {
    println(s"Math.floorDiv(-10, 5) = ${Math.floorDiv(-10, 5)}"); 
    println(s"Math.floorDiv(20, 6) = ${Math.floorDiv(20, 6)}"); 
    println("Math.floorDiv(20, Int.MaxValue) = "
            + Math.floorDiv(20, Int.MaxValue));
    println("Math.floorDiv(500, Long.MaxValue) = "
            + Math.floorDiv(500, Long.MaxValue));
  }
}

The output of the above code will be:

Math.floorDiv(-10, 5) = -2
Math.floorDiv(20, 6) = 3
Math.floorDiv(20, Int.MaxValue) = 0
Math.floorDiv(500, Long.MaxValue) = 0

❮ Scala - Math Methods