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


Ruby Integer ceil用法及代码示例


Ruby中的ceil函数以n位数字十进制数字的精度返回大于或等于int的最小数字。默认值被认为是0。如果给定的精度为负,则返回值是一个至少有ndigits.abs尾随零的整数。当n位数字为正数时,它将返回self。

用法:(number).ceil(ndigits)  

参数:该函数采用要返回其ceil值的整数和一个指定要返回的精度的单个参数ndigits。


返回值:该函数返回大于或等于它的最小整数。

范例1:

# Ruby program of ceil function 
  
# Initializing the numbers  
num1 = 10
num2 = 17
num3 = 17.5
num4 = 21.5
   
# Prints the ceil value 
puts num1.ceil 
puts num2.ceil(1) 
puts num3.ceil(0) 
puts num4.ceil(2)

输出

10
17.0
18
21.5

范例2:

# Ruby program of ceil function 
  
# Initializing the numbers  
num1 = 13
num2 = -90
num3 = 90
num4 = 81.7
   
# Prints the ceil value 
puts num1.ceil 
puts num2.ceil(2) 
puts num3.ceil(-1) 
puts num4.ceil(0)

输出

13
-90.0
90
82


相关用法


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