Ruby整数幅度函数与示例
Ruby 中的magnitude()函数返回整数的绝对值。它类似于 abs函数,是它的别名。
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.
示例 #1:
# Ruby program of Integer magnitude function
# Initializing the numbers
num1 = -21
num2 = 21
num3 = 0
num4 = -100
# Printing the magnitude value of integers
puts (num1).magnitude
puts (num2).magnitude
puts (num3).magnitude
puts (num4).magnitude
输出:
21
21
0
100
示例 #2:
# Ruby program of Integer magnitude function
# Initializing the numbers
num1 =29
num2 = -7
num3 = 90
num4 = -10
# Printing the magnitude value of integers
puts (num1).magnitude
puts (num2).magnitude
puts (num3).magnitude
puts (num4).magnitude
输出:
29
7
90
10