Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
communitycreator

What is Math.round() in Scala?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The round() function in Scala rounds a number off to the nearest integer. The image below shows a visual representation of the round() function.

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() returns Int.MaxValue or Log.MaxValue, accordingly.
  • If the parameter value is negative infinity, then round() returns Int.MinValue or Log.MinValue, accordingly.
  • If the parameter value is NaN, then round() returns 0.

Example

import scala.math._
object Main extends App {
//positive number
println(s"The value of round(11.2) = ${round(11.2)}");
//zero
println(s"The value of round(0) = ${round(0)}");
//negative number
println(s"The value of round(-10.6) = ${round(-10.6)}");
//already integer
println(s"The value of round(11) = ${round(11)}");
//error outputs
println(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)}");
}

RELATED TAGS

scala
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring