# Extending a string by adding 0's to the end

**URL:** <https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813>\
**Category:** New to Julia\
**Created:** [July 28, 2020, 12:28pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813 "2020-07-28T12:28:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 28, 2020, 12:28pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/1 "2020-07-28T12:28:37Z")

</div>

Suppose I have a process that receives strings like the following:

“1.542”  
“1.293”  
“1.652”  
“1.297”

Every now and again, however, the process receives a string with fewer “decimal places”. For example:

“1.9”

I wish to create a function that takes a given string received, checks if it has less than three “decimal places”, and if so, extends the string by adding 0’s to the end to return an updated string with three decimals. For example, inputting “1.9” to the function would return “1.900”

What Julia syntax is useful for this task?

---

<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:** [July 28, 2020, 12:54pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/3 "2020-07-28T12:54:58Z")

</div>

I had a similar problem in: [Generate equal length strings from numbers by padding with "0"? - #2 by fredrikekre](https://discourse.julialang.org/t/generate-equal-length-strings-from-numbers-by-padding-with-0/41365/2), except I needed padding on the left. There is also an `rpad` function available. I hope it helps.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 28, 2020, 1:07pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/4 "2020-07-28T13:07:15Z")

</div>

You may use `@sprintf` for this. It’s defined in `Printf`.

```julia
julia> @sprintf "%.3f" parse(Float64,"1.9")
"1.900"

```

Admittedly this is a somewhat roundabout way of going at it, and ideally you’d want a solution that does not convert to floating point.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 28, 2020, 1:23pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/5 "2020-07-28T13:23:44Z")

</div>

For padding integers with zeros on the left, you could also use `string`, although that was not mentioned in the cited thread.

> [@](#):
>
> ```
> string(n::Integer; base::Integer = 10, pad::Integer = 1)
> 
> ```
> 
> Convert an integer `n` to a string in the given `base`, optionally specifying a number of digits to pad to.
> 
> ```
> julia> string(5, base = 13, pad = 4)
> "0005"
> 
> julia> string(13, base = 5, pad = 4)
> "0023"
> 
> ```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 28, 2020, 1:46pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/6 "2020-07-28T13:46:16Z")

</div>

```julia
julia> using BenchmarkTools

julia> function rpad(s,c,l) while length(s)<l; s*=c; end; return s; end
rpad (generic function with 2 methods)

julia> function rpad2(s,c,l) add=l-length(s); if add<=0 return s; end; s*=repeat(c,add); return s; end
rpad2 (generic function with 1 method)

julia> s="1.1"
"1.1"

julia> @btime rpad($s,'0',10)
  387.685 ns (14 allocations: 448 bytes)
"1.10000000"

julia> @btime rpad2($s,'0',10)
  48.435 ns (2 allocations: 64 bytes)
"1.10000000"

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 28, 2020, 2:46pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/7 "2020-07-28T14:46:51Z")

</div>

Sorry, I am missing the context for these benchmarks: we already have `rpad`, are you suggesting that it could be improved?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 28, 2020, 2:54pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/8 "2020-07-28T14:54:50Z")

</div>

Oooops, no, i wasn’t sugesting that. I just read the OP, started to implement two versions, and checked their peformance and thought, OP would be interested as he wrotes

> [@Nash](#):
>
> I wish to create a function that takes a given string

I wasn’t very concentrated, sorry for making you (and probably others) puzzled. 😳

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 28, 2020, 4:02pm UTC](https://discourse.julialang.org/t/extending-a-string-by-adding-0s-to-the-end/43813/9 "2020-07-28T16:02:53Z")

</div>

Note two details:

1. [`rpad` in base](https://github.com/JuliaLang/julia/blob/ab05173c9539efb0aa82319f13672260a7b88076/base/strings/util.jl#L336) does virtually the same as oheil’s `rpad2` - except that it can take more than 1 character as “padding” string.
2. The OP didn’ specify if the left side of the numbers will always have one digit. If not, the `n` argument to `rpad` should be the sum of the number of decimals and the position of the decimal point (e.g. `findfirst('.', s)`).
