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


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

浮點數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



相關用法


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