Ruby Float coerce() 方法与示例
coerce()是一个浮点类方法,它返回传递的浮点值的 ceil 值。
Syntax: float.coerce()
Parameter: numeric values (can be integer as well as float value)
Return: An array with both float and numeric value represented in the form of Float objects.
示例 #1:
# Ruby code for coerce() method
# ceil value of the float value
puts "coerce example 1 : #{2.5.coerce(1.1)}\n\n"
# ceil value of the float value
puts "coerce example 2 : #{2.11115.coerce(22221)}\n\n"
输出 :
coerce example 1 : [1.1, 2.5]
coerce example 2 : [22221.0, 2.11115]
示例 #2:
# Ruby code for coerce() method
# ceil value of the float value
puts "coerce example 1 : #{2.00.coerce(1.1)}\n\n"
# ceil value of the float value
puts "coerce example 2 : #{-333333.coerce(2.21)}\n\n"
输出 :
coerce example 1 : [1.1, 2.0]
coerce example 2 : [2.21, -333333.0]