Ruby 整数 to_int函数与示例
Ruby 中的to_int函数将数字的值转换为 int。如果数字本身是一个 int,它只返回 self。它是 to_i函数的别名。
Syntax: number.to_int
Parameter: The function takes the number which is to be converted to int.
Return Value: The function returns the int value.
示例 #1:
Ruby
# Ruby program for to_int function
# Initializing the number
num1 = 51
num2 = 10.78
num3 = 1690.89
num4 = 183
# Prints the number as int
puts num1.to_int
puts num2.to_int
puts num3.to_int
puts num4.to_int
Ruby
# Ruby program for to_int function
# Initializing the number
num1 = 312
num2 = 98.09
num3 = 190.23
num4 = 1312
# Prints the number as int
puts num1.to_int
puts num2.to_int
puts num3.to_int
puts num4.to_int
输出:
51
10
1690
183
示例 #2:
红宝石
# Ruby program for to_int function
# Initializing the number
num1 = 312
num2 = 98.09
num3 = 190.23
num4 = 1312
# Prints the number as int
puts num1.to_int
puts num2.to_int
puts num3.to_int
puts num4.to_int
输出:
312
98
190
1312