# Why is big(0.5) printed as 0.50?

**URL:** <https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395>\
**Category:** General Usage\
**Tags:** display, show, bigfloat\
**Created:** [April 16, 2021, 9:12am UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395 "2021-04-16T09:12:46Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [April 16, 2021, 9:12am UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395/1 "2021-04-16T09:12:46Z")

</div>

I’m not sure if this is the intended behavior:

```julia
julia> 0.5
0.5

julia> big(0.5)
0.50

julia> big(0.5) == 0.5
true

```

Why the extra `0` at the end for a `BigFloat`?

---

<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:** [April 16, 2021, 9:22am UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395/2 "2021-04-16T09:22:32Z")

</div>

Output is done by the show function which is different for the two types:

```julia
julia> @which show(stdout,big(0.5))
show(io::IO, b::BigFloat) in Base.MPFR at mpfr.jl:1002

julia> @which show(stdout,0.5)
show(io::IO, x::T) where T<:Union{Float16, Float32, Float64} in Base.Ryu at ryu\Ryu.jl:111

```

This is show for BigFloat:

```julia
function show(io::IO, b::BigFloat)
    if get(io, :compact, false)
        print(io, _string(b, 5))
    else
        print(io, _string(b))
    end
end

```

And this for standard Float64:

```julia
function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
    compact = get(io, :compact, false)::Bool
    buf = Base.StringVector(neededdigits(T))
    typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
    pos = writeshortest(buf, 1, x, false, false, true, -1,
        (x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
    write(io, resize!(buf, pos - 1))
    return
end

```

I didn’t checked now for the specific additional `0` in the bigFloat case, but you can play around with both implementations to explore the differences.

---

<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:** [April 16, 2021, 9:24am UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395/3 "2021-04-16T09:24:56Z")

</div>

For the bigFloat you also need (from the same file):

```julia
function _string(x::BigFloat, fmt::String)::String
    isfinite(x) || return string(Float64(x))
    _prettify_bigfloat(string_mpfr(x, fmt))
end
_string(x::BigFloat) = _string(x, "%.Re")
_string(x::BigFloat, k::Integer) = _string(x, "%.$(k)Re")

string(b::BigFloat) = _string(b)

print(io::IO, b::BigFloat) = print(io, string(b))

```

but you can open those files on your own to get all needed dependencies, like  
`_prettify_bigfloat(s::String)::String`

---

<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:** [April 16, 2021, 10:06am UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395/4 "2021-04-16T10:06:50Z")

</div>

I realize that the methods are different, but my question was if this behavior is intended or if it is a bug in the display format.

---

<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:** [April 16, 2021, 10:43am UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395/5 "2021-04-16T10:43:37Z")

</div>

I don’t know, probably not intended that there is some special reasoning behind it. But that doesn’t make it a bug. I would say: it’s not important enough to deal with it in a priorized way.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 16, 2021, 11:46am UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395/6 "2021-04-16T11:46:06Z")

</div>

The string is rendered by MPFR as “5.0e-1”. Julia gets rid of the “e” notation by moving the decimal point.

---

<div class="post-metadata">

**Author:** ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)\
**Post date:** [April 17, 2021, 5:06pm UTC](https://discourse.julialang.org/t/why-is-big-0-5-printed-as-0-50/59395/7 "2021-04-17T17:06:04Z")

</div>

For convenience/reference, the code that does that is [here](https://github.com/JuliaLang/julia/blob/0b0e8e598ad63467c878011492c80e5f12be4ad7/base/mpfr.jl#L964-L990):

```julia
function _prettify_bigfloat(s::String)::String
    mantissa, exponent = split(s, 'e')
    if !occursin('.', mantissa)
        mantissa = string(mantissa, '.')
    end
    mantissa = rstrip(mantissa, '0')
    if endswith(mantissa, '.')
        mantissa = string(mantissa, '0')
    end
    expo = parse(Int, exponent)
    if -5 < expo < 6
        expo == 0 && return mantissa
        int, frac = split(mantissa, '.')
        if expo > 0
            expo < length(frac) ?
                string(int, frac[1:expo], '.', frac[expo+1:end]) :
                string(int, frac, '0'^(expo-length(frac)), '.', '0')
        else
            neg = startswith(int, '-')
            neg == true && (int = lstrip(int, '-'))
            @assert length(int) == 1
            string(neg ? '-' : "", '0', '.', '0'^(-expo-1), int, frac)
        end
    else
        string(mantissa, 'e', exponent)
    end
end

```

From a brief study of this code, it seems like the line

```julia
string(neg ? '-' : "", '0', '.', '0'^(-expo-1), int, frac)

```

…could be tweaked to not include `frac` if it is zero, as is the case with 0.5.

Maybe that behavior is there for a reason, but I guess I could submit a PR and see what feedback I get.

**Update** : I’ve submitted a PR: [mpfr.jl: don't add extra zero when pretty-printing BigFloats by waldyrious · Pull Request #40513 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/40513)
