📜  Ruby Floatrationalize() 方法与示例

📅  最后修改于: 2022-05-13 01:55:36.542000             🧑  作者: Mango

Ruby Floatrationalize() 方法与示例

Floatrationalize()是一个浮点类方法,它返回一个浮点值的简单有理形式 (p/q)。

示例 #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