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

  1. Get the digits of an integer:

    julia> digits(1234)
    4-element Array{Int64,1}:
    4
    3
    2
    1

    This example returns an array of the individual digits of the integer 1234.

  2. 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
    0

    Here, the binary representation of the number 1234 is obtained by setting the base argument to 2.

  3. 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
    0

    The pad argument is used to specify the desired size of the result. In this case, the result is padded with zeros to a length of 8.

  4. Specify the output element type:
    julia> digits(Float32, 1234)
    4-element Array{Float32,1}:
    4.0
    3.0
    2.0
    1.0

    By providing Float32 as the first argument, the resulting array will have the element type Float32.

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.

*Required Field
Details

Checking you are not a robot: