浮點數to_r()是一個浮點類方法,它返回浮點值的有理形式。
用法:float.to_r()
參數:浮點值作為參數
返回:浮點值的有理形式表示。
範例1:
# Ruby code for to_r() method
# Initializing values
a = 2.0
b = 9.99991
# Printing result
puts "Rational form of a:#{a.to_r}\n\n"
puts "Rational Form of b:#{b.to_r}\n\n"
輸出:
Rational form of a:2/1 Rational Form of b:21990034643427/2199023255552
範例2:
# Ruby code for to_r() method
# Initializing value
a = 0.767
b = 2797999.011
# Printing result
puts "Rational form of a:#{a.to_r}\n\n"
puts "Rational Form of b:#{b.to_r}\n\n"
輸出:
Rational form of a:6908521828386341/9007199254740992 Rational Form of b:375541070202667/134217728
相關用法
- Ruby Float eql()用法及代碼示例
- Ruby Float arg()用法及代碼示例
- Ruby Float zero?()用法及代碼示例
- Ruby Float quo()用法及代碼示例
- Ruby Float nan?()用法及代碼示例
- Ruby Float next_float()用法及代碼示例
- Ruby Float numerator()用法及代碼示例
- Ruby Float divmod()用法及代碼示例
- Ruby Float angle()用法及代碼示例
- Ruby Float rationalize()用法及代碼示例
- Ruby Float absolute()用法及代碼示例
- Ruby Float phase()用法及代碼示例
- Ruby Float positive()用法及代碼示例
- Ruby Float prev_float()用法及代碼示例
- Ruby Float fdiv()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby Float to_r() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。