Ruby中的abs()函數返回整數的絕對值。
用法:(number).abs
參數:該函數采用要返回其絕對值的整數。
返回值:該函數返回整數的絕對值。
範例1:
# Ruby program Integer abs() function
# Initializing the numbers
num1 = -21
num2 = 21
num3 = 0
num4 = -100
# Printing the absolute value of integers
puts (num1).abs
puts (num2).abs
puts (num3).abs
puts (num4).abs
輸出:
21 21 0 100
範例2:
# Ruby program of Integer abs() function
# Initializing the numbers
num1 =29
num2 = -7
num3 = 90
num4 = -10
# Printing the absolute value of integers
puts (num1).abs
puts (num2).abs
puts (num3).abs
puts (num4).abs
輸出:
29 7 90 10
相關用法
- Ruby Integer integer?用法及代碼示例
- Ruby Integer div()用法及代碼示例
- Ruby Integer chr用法及代碼示例
- Ruby Integer odd?用法及代碼示例
- Ruby Integer lcm()用法及代碼示例
- Scala Integer -用法及代碼示例
- Ruby Integer next用法及代碼示例
- Ruby Integer even?用法及代碼示例
- Ruby Integer gcd()用法及代碼示例
- Ruby Integer succ()用法及代碼示例
- Ruby Integer truncate()用法及代碼示例
- Ruby Integer magnitude用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby Integer abs() function with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。