Ruby 整数 divmod()函数与示例
Ruby 中的divmod()函数返回整数除法值和余数。
Syntax: (number1).divmod(number2)
Parameter: The function takes two number number1 and number2 whose integer division and remainder is returned.
Return Value: The function returns the integer division value and the remainder.
示例 #1:
# Ruby program of Integer divmod() function
# Initializing the numbers
num1 = 15
num2 = 4
num3 = 10
num4 = 2
# Prints the integer division
# and its remainder
puts num1.divmod(num2)
puts
puts num3.divmod(num4)
输出:
3
3
5
0
示例 #2:
# Ruby program of Integer divmod() function
# Initializing the numbers
num1 = 19
num2 = 2
num3 = 28
num4 = 13
# Prints the integer division
# and its remainder
puts num1.divmod(num2)
puts
puts num3.divmod(num4)
输出:
9
1
2
2