Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
communitycreator

What is the abs() method in Scala?

Syed Muhammad Ali Mustafa

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 abs() method in Scala is used to return the absolute value of an integer. The absolute value of an integer is its non-negative value.

Syntax

The abs() method can be declared as shown in the code snippet below:

(integer).abs
  • integer: The integer whose absolute value is required.

Return value

The abs() method returns the absolute value of integer.

Example

Consider the code snippet below, which demonstrates the use of the abs() method.

object main {
def main(args: Array[String]) = {
val n1 = -2;
println("(-2).abs: " + (n1).abs);
val n2 = 2;
println("(2).abs: " + (n2).abs);
val n3 = 0;
println("(0).abs: " + (n3).abs);
val n4 = Double.NaN;
println("(NaN).abs: " + (n4).abs);
val n5 = Double.NegativeInfinity;
println("(NegativeInfinity).abs: " + (n5).abs);
val n6 = Double.PositiveInfinity;
println("(PositiveInfinity).abs: " + (n6).abs);
}
}

RELATED TAGS

scala
communitycreator

CONTRIBUTOR

Syed Muhammad Ali Mustafa

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