當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Integer digits用法及代碼示例

Ruby中的digits函數返回將數字轉換為參數中給定基數的數字位置。如果沒有提供此類參數,則默認基數為10。

用法:number.digits(base)

參數:該函數采用要轉換的整數。它采用一個非強製性的參數庫,要轉換成該參數庫。基數的最小值可以是2。


返回值:轉換後,該函數返回新行中的位數。

範例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


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby Integer digits function with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。