红宝石 |数字 to_int()函数
to_int()是 Ruby 中的一个内置方法,返回给定数字的整数部分。
Syntax: num.to_int()
Parameters: The function needs the number whose integer part is to be returned.
Return Value: It returns the integer part.
示例 1 :
# Ruby program for to_int()
# method in Numeric
# Initialize a number
num1 = 15.778
# Prints the integer part
puts num1.to_int()
输出:
15
示例 2 :
# Ruby program for to_int()
# method in Numeric
# Initialize a number
num1 = -19.86
# Prints the integer part
puts num1.to_int()
输出:
-19