Scala Tutorial Scala References

Scala - Math ceil() Method



The Scala Math ceil() method returns the next highest integer value by rounding up the specified number, if necessary. In other words, it rounds the fraction UP 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.

Note: The value of ceil(x) is exactly the value of - floor(-x).

Syntax

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

Parameters

x Specify a number.

Return Value

Returns the next highest integer value by rounding UP the specified number, if necessary.

Exception

NA.

Example:

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

import scala.math._

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

The output of the above code will be:

ceil(-10.3) = -10.0
ceil(-10.5) = -10.0
ceil(-10.7) = -10.0
ceil(5.3) = 6.0
ceil(5.5) = 6.0
ceil(5.7) = 6.0
ceil(Double.NaN) = NaN
ceil(Double.PositiveInfinity) = Infinity
ceil(Double.NegativeInfinity) = -Infinity

❮ Scala - Math Methods