Scala Tutorial Scala References

Scala - Math floor() Method



The Scala Math floor() method returns the next lowest integer value by rounding down the specified number, if necessary. In other words, it rounds the fraction DOWN of the given number. In special cases it returns the following:

  • If the argument value is already an integer, then the result is the same as the argument.
  • If the argument is NaN or infinity, then the result is the same as the argument.

Syntax

def floor(x: Double): Double = java.lang.Math.floor(x)

Parameters

x Specify a number.

Return Value

Returns the next lowest integer value by rounding DOWN the specified number, if necessary.

Exception

NA.

Example:

In the example below, floor() method is used to round the fraction DOWN of the specified number.

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"floor(-10.3) = ${floor(-10.3)}"); 
    println(s"floor(-10.5) = ${floor(-10.5)}"); 
    println(s"floor(-10.7) = ${floor(-10.7)}"); 
    println(s"floor(5.3) = ${floor(5.3)}"); 
    println(s"floor(5.5) = ${floor(5.5)}"); 
    println(s"floor(5.7) = ${floor(5.7)}"); 
    println(s"floor(Double.NaN) = ${floor(Double.NaN)}");
    println("floor(Double.PositiveInfinity) = "
            + floor(Double.PositiveInfinity));
    println("floor(Double.NegativeInfinity) = "
            + floor(Double.NegativeInfinity));
  }
}

The output of the above code will be:

floor(-10.3) = -11.0
floor(-10.5) = -11.0
floor(-10.7) = -11.0
floor(5.3) = 5.0
floor(5.5) = 5.0
floor(5.7) = 5.0
floor(Double.NaN) = NaN
floor(Double.PositiveInfinity) = Infinity
floor(Double.NegativeInfinity) = -Infinity

❮ Scala - Math Methods