Ruby Integer magnitude function with example
Last Updated :
07 Jan, 2020
The magnitude() function in Ruby returns the absolute value of the integer. It is similar to the abs function and is an alias of it.
Syntax: (number).magnitude
Parameter: The function takes the integer whose magnitude is to be returned.
Return Value: The function returns the absolute value of the integer.
Example #1:
num1 = - 21
num2 = 21
num3 = 0
num4 = - 100
puts (num1).magnitude
puts (num2).magnitude
puts (num3).magnitude
puts (num4).magnitude
|
Output:
21
21
0
100
Example #2:
num1 = 29
num2 = - 7
num3 = 90
num4 = - 10
puts (num1).magnitude
puts (num2).magnitude
puts (num3).magnitude
puts (num4).magnitude
|
Output:
29
7
90
10
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...