當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Integer abs()用法及代碼示例

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


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby Integer abs() function with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。