Scala Tutorial Scala References

Scala - Math rint() Method



The Scala Math rint() method returns the Double value that is closest to the argument and equal to a mathematical integer. In special cases it returns the following:

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

Syntax

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

Parameters

x Specify a Double value.

Return Value

Returns the Double value that is closest to the argument and equal to a mathematical integer.

Exception

NA.

Example:

In the example below, rint() method returns the Double value that is closest to the given argument and equal to a mathematical integer.

import scala.math._

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

The output of the above code will be:

rint(-10.3) = -10.0
rint(-10.5) = -10.0
rint(-10.7) = -11.0
rint(5.3) = 5.0
rint(5.5) = 6.0
rint(5.7) = 6.0
rint(0.0) = 0.0
rint(Double.NaN) = NaN
rint(Double.PositiveInfinity) = Infinity
rint(Double.NegativeInfinity) = -Infinity

❮ Scala - Math Methods

5