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


Ruby Float absolute()用法及代碼示例


浮點數abs()是一個浮點類方法,它對浮點值起作用並將其轉換為絕對形式。

用法:float.abs()

參數:將轉換為絕對值的浮點值


返回:傳遞的浮點值的絕對值

範例1:

# Ruby code for abs() method 
  
# Initializing value 
a = -56.23333333
b = -10000.0
c = -(22 + 7.1) 
  
# Printing result 
puts "absolute value of a:#{a.abs}\n\n"
puts "absolute value of b:#{b.abs}\n\n"
puts "absolute value of c:#{c.abs}\n\n"

輸出:

absolute value of a:56.23333333

absolute value of b:10000.0

absolute value of c:29.1

範例2:

# Ruby code for abs() method 
  
# Initializing value 
a = -100.7 - 10.4
b = -100 * 2000.0
c = -(22 + 7.1) * 4
  
# Printing result 
puts "absolute value of a:#{a.abs}\n\n"
puts "absolute value of b:#{b.abs}\n\n 
puts "absolute value of c:#{c.abs}\n\n"

輸出:

absolute value of a:111.10000000000001

absolute value of b:200000.0

absolute value of c:116.4


相關用法


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