浮點數rationalize()是一個浮點類方法,它返回浮點值的簡單有理形式(p /q)。
用法: float.rationalize()
參數:浮點值作為參數
返回:簡單近似值
範例1:
# Ruby program for rationalize() method
# Initialize value
a = 0.767
b = 2999.011
# Printing result
puts "Rationalizing a:#{a.rationalize}\n\n"
puts "Rationalizing b:#{b.rationalize}\n\n"
輸出:
Rationalizing a:767/1000 Rationalizing b:2999011/1000
範例2:
# Ruby code for rationalize() method
# Initialize value
a = 0.767
b = 2999.011
c = 2.0000
# Printing result
puts "Rationalizing a:#{a.rationalize(0.01)}\n\n"
puts "Rationalizing b:#{b.rationalize(0.00001)}\n\n"
puts "Rationalizing c:#{c.rationalize(0.1)}\n\n"
輸出:
Rationalizing a:10/13 Rationalizing b:1634461/545 Rationalizing c:2/1
相關用法
- Ruby Rational rationalize()用法及代碼示例
- Ruby Float quo()用法及代碼示例
- Ruby Float arg()用法及代碼示例
- Ruby Float nan?()用法及代碼示例
- Ruby Float zero?()用法及代碼示例
- Ruby Float eql()用法及代碼示例
- Ruby Float next_float()用法及代碼示例
- Ruby Float numerator()用法及代碼示例
- Ruby Float coerce()用法及代碼示例
- Ruby Float positive()用法及代碼示例
- Ruby Float divmod()用法及代碼示例
- Ruby Float angle()用法及代碼示例
- Ruby Float prev_float()用法及代碼示例
- Ruby Float phase()用法及代碼示例
- Ruby Float denominator()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby Float rationalize() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。