# Compact float printing in the REPL

**URL:** <https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561>\
**Category:** General Usage\
**Created:** [June 30, 2017, 10:23am UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561 "2017-06-30T10:23:44Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [June 30, 2017, 10:23am UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/1 "2017-06-30T10:23:44Z")

</div>

How can I globally change the printing format of the REPL? I would specifically like to change the formatting of floats to a more compact style so that

```julia
julia> 10/3
3.3333

```

instead of

```julia
julia> 10/3
3.3333333333333335

```

I found [this issue](https://github.com/JuliaLang/julia/issues/6493) which was closed without resolution.

Looking at the code I see that

```julia
function Base.show(io::IO, x::Union{Float64,Float32})
    if get(io, :compact, false)
        _show(io, x, PRECISION, 6, true, true)
    else
        _show(io, x, SHORTEST, 0, true, false)
    end
end

```

refers to `:compact` but I don’t know how to set this. The [REPL formatting section](https://docs.julialang.org/en/stable/manual/environment-variables/#REPL-formatting-1) of the manual doesn’t mention it either.

---

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [August 31, 2018, 1:14am UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/2 "2018-08-31T01:14:12Z")

</div>

Did you ever find a nice answer to this?

---

<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:** [August 31, 2018, 3:32am UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/3 "2018-08-31T03:32:12Z")

</div>

from the docs (latest) search IOContext

```julia
julia> print(IOContext(stdout, :compact => false), 1.12341234)
1.12341234
julia> print(IOContext(stdout, :compact => true), 1.12341234)
1.12341

```

This does not change the way the REPL shows all Float64 values.

---

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [August 31, 2018, 12:53pm UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/4 "2018-08-31T12:53:32Z")

</div>

Thanks.

The reason that I am asking about this now is that I am doing demonstrations in class.  
For these, I would like to keep the syntax as simple as possible so that students can easily follow.  
But, I would also like to keep the numbers short.  
The projector in my classroom can’t handle high resolution!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [August 31, 2018, 1:23pm UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/5 "2018-08-31T13:23:26Z")

</div>

You can redefine the show method for float. `@which show(stdout, 2/3)` shows you where it is, `@less` prints it. This modification might work:

```julia
julia> function Base.show(io::IO, x::Union{Float64,Float32})
                 Base.Grisu._show(io, round(x, sigdigits=3), Base.Grisu.SHORTEST, 0, get(io, :typeinfo, Any) !== typeof(x), false)
         end                                                                                                                                                            
                                                                                                                                                                        
julia> 2/3                                                                                                                                                              
0.667                                                                                                                                                                   

julia> rand(10)                                                                                                                                                         
10-element Array{Float64,1}:                                                                                                                                            
 0.684                                                                                                                                                                  
 0.594                                                                                                                                                                  
 0.0643
 0.695                                                                                                                                                                  
 0.524                                                                                                                                                                  
 0.884                                                                                                                                                                  
 0.541                                                                                                                                                                  
 0.49                                                                                                                                                                   
 0.175                                                                                                                                                                  
 0.561                                                                                                                                                                  

```

(Note, there is a certain danger in redefining Base-methods! But should be ok for `show`)

---

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [September 2, 2018, 9:04pm UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/6 "2018-09-02T21:04:17Z")

</div>

Thanks!

That does just what I need.

---

<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:** [September 3, 2018, 6:31am UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/7 "2018-09-03T06:31:05Z")

</div>

Redefining `show` entirely seems a bit dramatic. I quoted the default `show` code above. Wouldn’t it be more appropriate to somehow change what is returned by this line:

```julia
get(io, :compact, false)

```

Can I can I somehow

```julia
set(stdout, :compact, true)

```

? This doesn’t work btw.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 3, 2018, 7:43am UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/8 "2018-09-03T07:43:49Z")

</div>

I am almost certain that there are better ways to do it, but I’m entirely certain that I don’t know those ways…

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [September 3, 2018, 9:56am UTC](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/9 "2018-09-03T09:56:41Z")

</div>

```julia
julia> 2/3
0.6666666666666666

julia> struct IOContextDisplay <: AbstractDisplay
           ctx
       end

julia> function Base.Multimedia.display(d::IOContextDisplay, x)
           io = IOBuffer()
           ctx = d.ctx(io)
           show(ctx, "text/plain", x)
           println(stdout, String(take!(io)))
       end

julia> disp = IOContextDisplay(x -> IOContext(x, :compact => true, :limit => true, :color => true))
IOContextDisplay(getfield(Main, Symbol("##3#4"))())

julia> pushdisplay(disp);

julia> 2/3
0.666667

```

This will probably break in some cases (and won’t work in Juno).
