What is the abs() method in Scala?
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);}}