Kotlin floorDiv()
The floorDiv()
function in Kotlin is used for integer division with flooring towards negative infinity. It returns the largest integer less than or equal to the algebraic quotient of the arguments.
In this tutorial, we shall learn the syntax and go through some examples of Kotlin floorDiv() function.
Syntax
The syntax of the floorDiv()
function is:
fun Byte.floorDiv(other: Byte): Int
fun Byte.floorDiv(other: Short): Int
fun Byte.floorDiv(other: Int): Int
fun Byte.floorDiv(other: Long): Long
fun Short.floorDiv(other: Byte): Int
fun Short.floorDiv(other: Short): Int
fun Short.floorDiv(other: Int): Int
fun Short.floorDiv(other: Long): Long
fun Int.floorDiv(other: Byte): Int
fun Int.floorDiv(other: Short): Int
fun Int.floorDiv(other: Int): Int
fun Int.floorDiv(other: Long): Long
fun Long.floorDiv(other: Byte): Long
fun Long.floorDiv(other: Short): Long
fun Long.floorDiv(other: Int): Long
fun Long.floorDiv(other: Long): Long
In summary, you can call floorDiv()
function on a Byte, Short, Int, or Long value, and pass any of the Byte, Short, Int, or Long value as argument.
Parameter | Description |
---|---|
other | The divisor. |
Examples
1. Using floorDiv() for Integer Division with Flooring
In this example, we’ll use floorDiv()
to perform integer division with flooring towards negative infinity.
Kotlin Program
fun main() {
val result = 10.floorDiv(3)
println("Result of 10 floor-divided by 3: $result")
}
Output
Result of 10 floor-divided by 3: 3
In this example, floorDiv()
is used to perform integer division with flooring towards negative infinity.
Summary
The floorDiv()
function in Kotlin is a convenient way to perform integer division with flooring towards negative infinity. It is particularly useful when dealing with mathematical operations that require integer results.