# Get length of a fully represented float

**URL:** <https://discourse.julialang.org/t/get-length-of-a-fully-represented-float/110521>\
**Category:** General Usage\
**Tags:** float\
**Created:** [February 21, 2024, 3:05pm UTC](https://discourse.julialang.org/t/get-length-of-a-fully-represented-float/110521 "2024-02-21T15:05:35Z")\
**Posts on this page:** 1\
**Showing post:** 13

<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:** [February 21, 2024, 7:42pm UTC](https://discourse.julialang.org/t/get-length-of-a-fully-represented-float/110521/13 "2024-02-21T19:42:57Z")

</div>

Try also code below using significant digits and based [on this other post](https://discourse.julialang.org/t/printf-significant-digits-in-floating-point-representation/29978/10).

```julia
using Printf

function full_float_signif(x::Float64, sigdig::Int)
    (x == 0) && (return (1, "0"))
    x = round(x, sigdigits=sigdig)
    n = length(@sprintf("%d", abs(x))) # length of the integer part
    if (x ≤ -1 || x ≥ 1)
        decimals = max(sigdig - n, 0) # 'sig - n' decimals needed 
    else
        Nzeros = ceil(Int, -log10(abs(x))) - 1 # No. zeros after decimal point before first number
        decimals = sigdig + Nzeros
    end
    s = @sprintf("%.*f", decimals, x)
    return length(s), s
end

# Example:
full_float_signif(1.87589e-17, 6) # (24, "0.0000000000000000187589")

```

---

_[View the full topic](https://discourse.julialang.org/t/get-length-of-a-fully-represented-float/110521)._
