# Find number of digits in a non-integer number?

**URL:** <https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445>\
**Category:** General Usage\
**Created:** [April 12, 2020, 12:24pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445 "2020-04-12T12:24:40Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 12, 2020, 12:24pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/1 "2020-04-12T12:24:40Z")

</div>

Hello!

Say I have four numbers; 1.0 0.5 0.05 0.065

I want to know how far I have to go before I hit the first non-zero value. So in this case;

For 1 I would not have to move, so I expect to get output, 0  
For 0.5 I would have to move once, so I expect to get output, 1  
For 0.05, I would have to move twice, so I expect to get output, 2

And lastly for 0.065, I would expect to get ouput, 2, as I did with 0.05.

I have tried using the “digits” and “ndigits” functionality but they seem to only work for integers - does this kind of function I seek exist in Julia natively or do I have to do something with string manipulation?

Kind regards

---

<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:** [April 12, 2020, 12:47pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/2 "2020-04-12T12:47:06Z")

</div>

Although it is not exposed API so it may change, you may find the second value from `Base.Ryu.reduce_shortest` useful.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 12, 2020, 12:58pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/3 "2020-04-12T12:58:41Z")

</div>

```julia
ilog10(x) = Int(floor(log10(x)))
alog10(x) = abs(ilog10(x))

to_nonzero_digit(x::Integer) = 0

function to_nonzero_digit(x::AbstractFloat)
    !isfinite(x) && throw(DomainError(x, "expected a finite value"))
    if isinteger(x)
        0
    else
        alog10(abs(x))
    end
end

```

```julia
ns=(1.0, 0.5, 0.05, 0.0625, 0.001, 0.0025)
to_nonzero_digit.(ns) == (0, 1, 2, 2, 3, 3)

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 12, 2020, 1:19pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/4 "2020-04-12T13:19:33Z")

</div>

I was not able to find it, maybe because I am using Julia v1.2 or that I did not import it properly?

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 12, 2020, 1:19pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/5 "2020-04-12T13:19:52Z")

</div>

Thanks, exactly what I wanted

Kind regards

---

<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:** [April 12, 2020, 1:32pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/6 "2020-04-12T13:32:00Z")

</div>

It is more recent (you need at least 1.3, but then why not go to 1.4). The result should be identical to @JeffreySarnoff’s solution though.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 12, 2020, 2:29pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/7 "2020-04-12T14:29:33Z")

</div>

I see, thanks for making me aware of it 🙂 Just downloaded v1.4.

Kind regards

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [April 12, 2020, 9:39pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/8 "2020-04-12T21:39:29Z")

</div>

In older Julia versions, `x -> 1 - Base.Grisu.grisu(x, Base.Grisu.PRECISION, 4)[2]` would serve the same purpose I believe.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 13, 2020, 5:58am UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/9 "2020-04-13T05:58:47Z")

</div>

Thanks for the suggestion!

I ended up making a modified version, as such;

```julia
to_nonzero_digit(x::Integer) = 0

function to_nonzero_digit(x::AbstractFloat)
    !isfinite(x) && throw(DomainError(x, "Expected a finite value"))
    test = 1 - Base.Grisu.grisu(x, Base.Grisu.PRECISION, 4)[2];
    if test < 0
        val = 0;
    else
        val = test;
    end
end

```

Which should work for any number, atleast the ones in the range of 0 to 100.

Kind regards

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [April 13, 2020, 8:30am UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/10 "2020-04-13T08:30:45Z")

</div>

> [@Ahmed\_Salih](#):
>
> to\_nonzero\_digit(x::Integer) = 0

Does `to_nonzero_digit(0)` work for you or should you special case it / through an error?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 13, 2020, 8:32am UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/11 "2020-04-13T08:32:38Z")

</div>

Due to an error, since the other method only takes in an abstract float - if you input “2” without this special case, then it would error out.

Kind regards

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [April 13, 2020, 8:38am UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/12 "2020-04-13T08:38:09Z")

</div>

That is not what I mean, I wonder if `to_nonzero_digit(0)` should return something else than `0`?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 13, 2020, 8:45am UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/13 "2020-04-13T08:45:45Z")

</div>

Oh sorry, read to fast. Yes, ideally it should throw an error, since in my use case it should not be allowed. That would be good to add to it actually.

The only values for x which should be allowed are real positive numbers, is there a smart way to specify that condition?

Kind regards

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 22, 2020, 5:34pm UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/15 "2020-12-22T17:34:51Z")

</div>

Not as elegant as that of Jeffrey S., but perhaps more performing, if it were relevant

```julia
function count_dgtz(x)
    zs=0.
    while x <1.
        x*=10.
        zs+=1
    end
    return zs
end

```

a few different tips, using string functions, which are much slower:

```julia
function count_dgtz2(x)
    x=string(x)
    tot=replace(x,'.'=>"")
    lt=length(tot)
    lnz=length(lstrip(tot,'0'))
    return lt-lnz
end

```

```julia
function count_dgtz3(x) 
    x>1 ? 0 : findfirst(x->x>'0',string(x))-2
end

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 23, 2020, 8:04am UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/16 "2020-12-23T08:04:03Z")

</div>

I have experienced that the solutions that use string functions have the problem of “translation” in exponential format of some real numbers.

```julia
julia> function count_dgtz3(x)
           x>1 ? 0 : findfirst(x->x>'0',string(x))-2
           end
count_dgtz3 (generic function with 1 method)

julia> x=0.00000000060007
6.0007e-10

julia> count_dgtz3(x)
-1

julia> x=0.0060007
0.0060007

julia> count_dgtz3(x)
3

```

Is it possible somehow to get a string with the same form “0.000000000007” from a number 0.000000000007?

What follow would be a workaround to overcome the exponential format problem

```julia
function count_dgtz4(x)
    if x > 1
        return 0
    elseif x <10.e-5
        return parse(Int,last(split(string(x),'-')))
    else
        s=string(x)
        return match(r"(0[1-9])|(\.[1-9])", s).offset-1
    end
end

julia> x=0.00000000060007
6.0007e-10

julia> count_dgtz4(x)
10

```

---

<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:** [December 23, 2020, 8:13am UTC](https://discourse.julialang.org/t/find-number-of-digits-in-a-non-integer-number/37445/17 "2020-12-23T08:13:16Z")

</div>

@rocco_sprmnt21, you could use: `@sprintf`
