round()是Ruby中的内置函数,返回有理数舍入到最接近的值,精度为n位十进制数字。 ndigits默认为0。当ndigits为正数时,它返回有理数,否则返回整数。
用法: rational.round(ndigits)
参数:该函数接受单个参数
返回值:返回有理数舍入到最接近的值,精度为n位十进制数字。
例子1:
# Ruby program for round() method 
  
# Initialize rational number 
rat1 = Rational(-4, 3) 
  
# Prints the rational number 
puts rat1.round()输出:
-1
例子2:
# Ruby program for round() method 
  
# Initialize rational number 
rat1 = Rational('123.456') 
  
# Prints the rational number 
puts rat1.round(1) 
puts rat1.round(-1)输出:
247/2 120
相关用法
- Ruby Rational abs()用法及代码示例
- Ruby Rational quo()用法及代码示例
- Ruby Rational floor()用法及代码示例
- Ruby Rational to_f()用法及代码示例
- Ruby Rational to_i()用法及代码示例
- Ruby Rational to_s()用法及代码示例
- Ruby Rational to_r()用法及代码示例
- Ruby Rational truncate()用法及代码示例
- Ruby Rational denominator()用法及代码示例
- Ruby Rational fdiv()用法及代码示例
- Ruby Rational ceil()用法及代码示例
- Ruby Rational numerator()用法及代码示例
- Ruby Rational rationalize()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Ruby | Rational round() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
