# Generate equal length strings from numbers by padding with "0"?

**URL:** <https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365>\
**Category:** General Usage\
**Created:** [June 14, 2020, 11:21am UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365 "2020-06-14T11:21:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [June 14, 2020, 11:21am UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/1 "2020-06-14T11:21:18Z")

</div>

Let’s say I have an array of integers x = [1, 14, 356].  
I want to generate individual strings, with equal lengths: x\_str = [“001”, “014”, “356”].  
How can I achieve that?

The background of the problem is the following: I started playing with [CImGui.jl](https://github.com/Gnimuc/CImGui.jl) (it’s awesome!) and I want to automatically generate checkboxes which will be used to edit frames for an SPI communication.  
I managed to create the checboxes, but because of the variable length of their names, they are not aligned horizontally.

I could search through the ImGui documentation on how to align them, but since I thought of this workaround I’m curious if there’s a simple(one line-ish)\_ solution to generate equal length strings by padding with “0”.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 14, 2020, 11:29am UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/2 "2020-06-14T11:29:38Z")

</div>

```julia
julia> x = [1, 14, 356];

julia> function pad_zeros(x)
           strs = string.(x)
           m = maximum(length, strs)
           lpad.(x, m, "0")
       end
pad_zeros (generic function with 1 method)

julia> pad_zeros(x)
3-element Array{String,1}:
 "001"
 "014"
 "356"

```

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [June 14, 2020, 11:43am UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/3 "2020-06-14T11:43:41Z")

</div>

Thank you! This is exactly what I needed! I didn’t know the `lpad` function.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [June 14, 2020, 12:18pm UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/4 "2020-06-14T12:18:16Z")

</div>

Actually `lpad` does the “stringification” behind the scenes 😉

```julia
julia> x = [1, 14, 356]
3-element Array{Int64,1}:
   1
  14
 356

julia> [lpad(_x, 3, "0") for _x ∈ x]
3-element Array{String,1}:
 "001"
 "014"
 "356"

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [June 14, 2020, 12:45pm UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/5 "2020-06-14T12:45:44Z")

</div>

But where did your `3` come from? I think it’s worth converting them to strings for the sake of:

```julia
m = maximum(length, strs)

```

Although, this may yield better performance

```julia
function pad_zeros(x)
    m = floor(Int, maximum(y -> y > 0 ? log10(y) : log10(-y)+1, x)) + 1
    lpad.(x, m, "0")
end

```

(But I find this less readable, and I’d be extremely surprised if lpad is a performance bottle neck…)

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [June 14, 2020, 12:53pm UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/6 "2020-06-14T12:53:07Z")

</div>

Yep, the `3` was just to demonstrate the approach. Of course a non-hardcoded version is preferable, always 😉

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [June 14, 2020, 1:07pm UTC](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/7 "2020-06-14T13:07:59Z")

</div>

The `[lpad(_x, 3, "0") for _x ∈ x]` with hardcoded number of zeros will also work because my application will never use bitfields larger than 64 bits. Of course, using the `pad_zeros` function is more future proof. Performance is not critical for this application.
