# Trailing zeros

**URL:** <https://discourse.julialang.org/t/trailing-zeros/53549>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 18, 2021, 5:19pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549 "2021-01-18T17:19:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![saraflgarcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saraflgarcia/32/14847_2.png) [@saraflgarcia](https://discourse.julialang.org/u/saraflgarcia)\
**Post date:** [January 18, 2021, 5:19pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/1 "2021-01-18T17:19:05Z")

</div>

Hi everyone,

I want to convert a number to a string, but keeping the trailing zeros:  
string(1) \> “1”  
string(1.0) \> “1.0”  
string(1.00) \> “1.0” - How do I get the result “1.00”?

Thanks to all.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2021, 5:20pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/2 "2021-01-18T17:20:54Z")

</div>

Use Printf and then sprintf.

---

<div class="post-metadata">

**Author:** ![saraflgarcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saraflgarcia/32/14847_2.png) [@saraflgarcia](https://discourse.julialang.org/u/saraflgarcia)\
**Post date:** [January 18, 2021, 5:51pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/3 "2021-01-18T17:51:24Z")

</div>

Thanks, but the issue is that we don’t know a priori the number of decimals,  
It should work as follows

string\_to\_number(n) =  
string(n)

And this should result in a string with exact same number of decimals of n

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 18, 2021, 6:01pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/4 "2021-01-18T18:01:45Z")

</div>

The problem is that the computer’s floating-point representation of numbers doesn’t include any information about how many zeros the user typed, so we can’t reconstruct that information for the output. It might help to know the broader context of what you’re trying to do - can you expand on your use-case?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 18, 2021, 6:01pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/5 "2021-01-18T18:01:54Z")

</div>

…

I think there is some confusion on concepts here.

In Julia, and most other languages, `1.0 == 1.00`, the two are the exact same value and it is impossible to distinguish between them inside a program. Julia (and many other languages) do not save anywhere the number of decimal places that were explicitly denoted in the literal was first used for it.

---

<div class="post-metadata">

**Author:** ![saraflgarcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saraflgarcia/32/14847_2.png) [@saraflgarcia](https://discourse.julialang.org/u/saraflgarcia)\
**Post date:** [January 18, 2021, 6:07pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/6 "2021-01-18T18:07:25Z")

</div>

Thanks for the replies.  
The use case is, given a range, trying to find a step based on typed decimal cases, e.g.:  
[0,1] \> step=1  
[0.0, 1.0] \> step=0.1  
[0.00, 1.00] \> step = 0.01  
…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2021, 6:11pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/7 "2021-01-18T18:11:08Z")

</div>

Your problem would make sense if the input is read from the terminal as strings. Is it the case?

---

<div class="post-metadata">

**Author:** ![saraflgarcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saraflgarcia/32/14847_2.png) [@saraflgarcia](https://discourse.julialang.org/u/saraflgarcia)\
**Post date:** [January 18, 2021, 6:16pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/8 "2021-01-18T18:16:32Z")

</div>

Well, the input is read from CSV files.  
Currently numbers are read as numbers, but I guess it won’t be hard to read them as strings.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 18, 2021, 6:21pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/9 "2021-01-18T18:21:14Z")

</div>

> [@saraflgarcia](#):
>
> [0.00, 1.00]

If that is typed as a string it is possible to write a cumbersome function to extract the number of decimals, such as:

```julia
julia> range = "[0.00, 1.00]"
"[0.00, 1.00]"

julia> length(split(split(replace(range, r"[^0-9.]" => ' '))[2],".")[2])
2

```

(there are probably more smart ways to do that with regular expressions)

---

<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:** [January 19, 2021, 11:52am UTC](https://discourse.julialang.org/t/trailing-zeros/53549/10 "2021-01-19T11:52:43Z")

</div>

Or something simple like

```julia
function count_after_dot(str)
    # assumes `str` is a valid number
    seen_dot = false
    trailing = 0
    for c in str
        if c ≡ '.'
            seen_dot = true
        else
            trailing += seen_dot
        end
    end
    trailing
end

julia> count_after_dot("1.00")
2

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 19, 2021, 3:17pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/11 "2021-01-19T15:17:08Z")

</div>

Another simple solution:

```julia
str = ["1", "1.", "1.0", "1.00", "1.000"]
[occursin(".", s) ? length(split(s,".")[2]) : 0 for s in str]
 0
 0
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 19, 2021, 3:27pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/12 "2021-01-19T15:27:45Z")

</div>

And another solution (non allocating)

```julia
str = ["1", "1.", "1.0", "1.00", "1.000"]
[findfirst('.', x) |> z -> z === nothing ? 0 : length(x) - z for x in str]

```

---

<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:** [January 19, 2021, 3:55pm UTC](https://discourse.julialang.org/t/trailing-zeros/53549/13 "2021-01-19T15:55:27Z")

</div>

This really doesn’t seem like the right way to specify stepsize, it seems highly unorthodox. Why not clearly specify stepsize explicitly?

---

<div class="post-metadata">

**Author:** ![saraflgarcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saraflgarcia/32/14847_2.png) [@saraflgarcia](https://discourse.julialang.org/u/saraflgarcia)\
**Post date:** [January 20, 2021, 10:39am UTC](https://discourse.julialang.org/t/trailing-zeros/53549/14 "2021-01-20T10:39:55Z")

</div>

Thanks for the replies.  
Yes, specify the stepsize is an option, this ‘unorthodox’ one is only for having a default size if the user does not specify any.
