红宝石 |数值 abs()函数
abs是 Ruby 中的一个内置方法,它返回一个数字的绝对值。
Syntax: num.abs
Parameters: The function needs the number whose absolute value is to be returned.
Return Value: It returns the absolute value of a numbers.
示例 1 :
Ruby
# Ruby program for abs() method in Numeric
# Initialize two numbers
num = -19
# Prints absolute value of num
puts num.abs()
Ruby
# Ruby program for abs() method in Numeric
# Initialize two numbers
num = 100
# Prints absolute value of num
puts num.abs()
输出:
19
示例 2 :
红宝石
# Ruby program for abs() method in Numeric
# Initialize two numbers
num = 100
# Prints absolute value of num
puts num.abs()
输出:
100