# Leading Zeros

**URL:** <https://discourse.julialang.org/t/leading-zeros/30450>\
**Category:** General Usage\
**Created:** [October 29, 2019, 2:26pm UTC](https://discourse.julialang.org/t/leading-zeros/30450 "2019-10-29T14:26:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [October 29, 2019, 2:26pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/1 "2019-10-29T14:26:11Z")

</div>

Hi,

I have a simple question but i cannot find the answer. I have an array of integers between 1 to 999. I need the left pading of 3 so that every element is composed of 3 digits.E.g. [001, 002,…090…, 900]. But I would strongly prefer them to be Integers, LPAD() outputs strings.

Is that possible?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 29, 2019, 2:29pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/2 "2019-10-29T14:29:06Z")

</div>

Looks like you can use the `lpad` function

```julia
lpad(string(n), 3, '0')

```

You can quickly run this on an entire array with broadcasting

```julia
lpad.(string.(v), 3, '0')

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [October 29, 2019, 2:30pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/3 "2019-10-29T14:30:18Z")

</div>

Where do you want this to happen? When the array is printed?

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [October 29, 2019, 2:35pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/4 "2019-10-29T14:35:39Z")

</div>

I don’t think that’s possible since `3 == 003`. If you have the bitstring for an integer `00000011` is that 3 or 03 or 003…?

Of course, there are ways around that, using a string, making a custom type with the number of leading zeros in it, storing the digits in an array (the `digits`-function) etc. But none of these are integers.

Maybe we can help you find a good solution if you tell us a little more about why you want it exactly this way; probably there is a good alternative.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 29, 2019, 2:38pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/5 "2019-10-29T14:38:27Z")

</div>

Sorry, now I think I was confused by the question. I would agree with @giordano the only solution is to do this wherever the output is happening. You don’t want to just make all integers display that way by default, trust me.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [October 29, 2019, 4:29pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/6 "2019-10-29T16:29:55Z")

</div>

Hi,

Thanks for the fast reply! if I left pad it to “002”. How can I explode it so it is [‘0’,‘0’,‘2’]?

best,

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 29, 2019, 4:33pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/7 "2019-10-29T16:33:28Z")

</div>

```julia
julia> digits(2, pad = 3)
3-element Array{Int64,1}:
 2
 0
 0

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 29, 2019, 4:51pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/8 "2019-10-29T16:51:56Z")

</div>

`collect("002")`

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 29, 2019, 4:59pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/9 "2019-10-29T16:59:16Z")

</div>

Although, if you have a number and want digits as numbers, there’s no need to go through strings.

---

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [October 29, 2019, 6:33pm UTC](https://discourse.julialang.org/t/leading-zeros/30450/10 "2019-10-29T18:33:11Z")

</div>

wasn’t aware of digits() could work this way as well 🙂 thanks!

---

<div class="post-metadata">

**Author:** ![pontus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pontus/32/10438_2.png) [@pontus](https://discourse.julialang.org/u/pontus)\
**Post date:** [October 30, 2019, 3:20am UTC](https://discourse.julialang.org/t/leading-zeros/30450/11 "2019-10-30T03:20:03Z")

</div>

```julia
using Printf
for i in 1:999
    @printf("%03d\n", i)
end
001
002
003 ...

```

Can also render padded binary output:

```nohighlight
    @printf("%6i → %s → %03d\n", i, string(i, base=2, pad=11), i)
end
     1 → 00000000001 → 001
     2 → 00000000010 → 002
     3 → 00000000011 → 003
     4 → 00000000100 → 004
     5 → 00000000101 → 005
     6 → 00000000110 → 006
     7 → 00000000111 → 007 ...

```
