digits
digits([T], n, [base], [pad])
Returns an array with element type T (default Int) of the digits of n in the given base, optionally padded with zeros to a specified size. More significant digits are at higher indexes, such that n == sum([digits[k]*base^(k-1) for k=1:length(digits)]).
Examples
-
Get the digits of an integer:
julia> digits(1234) 4-element Array{Int64,1}: 4 3 2 1This example returns an array of the individual digits of the integer
1234. -
Specify the base of the number:
julia> digits(1234, base=2) 11-element Array{Int64,1}: 0 1 0 0 1 1 0 1 0 0 0Here, the binary representation of the number
1234is obtained by setting thebaseargument to 2. -
Pad the result with zeros to a specific size:
julia> digits(1234, pad=8) 8-element Array{Int64,1}: 0 0 0 1 2 3 4 0The
padargument is used to specify the desired size of the result. In this case, the result is padded with zeros to a length of 8. - Specify the output element type:
julia> digits(Float32, 1234) 4-element Array{Float32,1}: 4.0 3.0 2.0 1.0By providing
Float32as the first argument, the resulting array will have the element typeFloat32.
Common mistake example:
julia> digits(1234, base=1)
ERROR: ArgumentError: base must be >= 2
In this example, an error is thrown because the base provided is less than 2. The base argument must be a positive integer greater than or equal to 2.
See Also
digits, inf, isdigit, iseven, isfinite, isless, islower, isnumber, isodd, isprime, isqrt, issorted, issubnormal, isxdigit, nan,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.