浮點數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
相關用法
- Ruby Float eql()用法及代碼示例
- Ruby Float nan?()用法及代碼示例
- Ruby Float quo()用法及代碼示例
- Ruby Float zero?()用法及代碼示例
- Ruby Float arg()用法及代碼示例
- Ruby Float to_i()用法及代碼示例
- Ruby Float numerator()用法及代碼示例
- Ruby Float negative()用法及代碼示例
- Ruby Float positive()用法及代碼示例
- Ruby Float phase()用法及代碼示例
- Ruby Float denominator()用法及代碼示例
- Ruby Float angle()用法及代碼示例
- Ruby Float fdiv()用法及代碼示例
- Ruby Float floor()用法及代碼示例
- Ruby Float ceil()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby Float absolute() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。