What is Math.round() in Scala?
The round() function in Scala rounds a number off to the nearest integer. The image below shows a visual representation of the round() function.
The following module is required for this function:
import scala.math._
Syntax
Int round(Float number)
Long round(Double number)
Parameter
number: the parameter to be rounded off.
Return value
The round() function returns the nearest integer value of a number.
- If the parameter value is positive infinity, then
round()returnsInt.MaxValueorLog.MaxValue, accordingly.- If the parameter value is negative infinity, then
round()returnsInt.MinValueorLog.MinValue, accordingly.- If the parameter value is
NaN, thenround()returns 0.
Example
import scala.math._object Main extends App {//positive numberprintln(s"The value of round(11.2) = ${round(11.2)}");//zeroprintln(s"The value of round(0) = ${round(0)}");//negative numberprintln(s"The value of round(-10.6) = ${round(-10.6)}");//already integerprintln(s"The value of round(11) = ${round(11)}");//error outputsprintln(s"The value of round(Double.PositiveInfinity) = ${round(Double.PositiveInfinity)}");println(s"The value of round(Double.NegativeInfinity) = ${round(Double.NegativeInfinity)}");println(s"The value of round(Double.NaN) = ${round(Double.NaN)}");}