Ruby 整数数字函数与示例
Ruby 中的digits函数返回将数字转换为参数中给定基数的数字位置。如果没有提供此类参数,则默认基数为 10。
Syntax: number.digits(base)
Parameter: The function takes the integer whose conversion is to be done. It takes a non-mandatory parameter base to which the conversion is to be done. The minimum value in the base can be 2.
Return Value: The function returns the digits place in new line after conversion.
示例 #1:
# Ruby program of Integer digits function
# Initializing the numbers
num1 = 6788
num2 = 89
# Prints the number
# after base conversion
puts num1.digits
puts
puts num2.digits(3)
输出:
8
8
7
6
2
2
0
0
1
示例 #2:
# Ruby Program of Integer digits function
# Initializing the numbers
num1 = 563 num2 = 12
# Prints the number
# after base conversion
puts num1.digits
puts
puts num2.digits(6)
输出:
3
6
5
0
2