当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Float rationalize()用法及代码示例


浮点数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


相关用法


注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby Float rationalize() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。