# How do I restrict the number of digits in the REPL display?

**URL:** <https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501>\
**Category:** New to Julia\
**Tags:** repl, io, rounding\
**Created:** [December 12, 2023, 2:37pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501 "2023-12-12T14:37:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 12, 2023, 2:37pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/1 "2023-12-12T14:37:19Z")

</div>

I wish to restrict the number of significant digits displayed on the REPL according to a context-dependent format like `format` in MATLAB or Octave.

I have looked through the documentation, but have not seen what I am looking for.

Is this function available as standard or via a package?

---

<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 12, 2023, 4:00pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/2 "2023-12-12T16:00:00Z")

</div>

Check [this post](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/3) for a Matlab-like solution.

---

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 12, 2023, 5:42pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/3 "2023-12-12T17:42:51Z")

</div>

I have written a function to invoke within the REPL, called say, `display_digits` like so:

```julia
function display_digits(n)
    using Printf
    Base.show(io::IO, f::Float64) = @printf(io, "%.nf", f)
end

```

I have these questions:

1. Where should this function file reside so that Julia sees it automatically?
2. How should I invoke this function?
3. How might the function be improved? [AFAICT it does not work at present].

Thanks.

---

<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 12, 2023, 5:53pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/4 "2023-12-12T17:53:21Z")

</div>

To use a variable in the `Printf` format string, see for example [this post](https://discourse.julialang.org/t/printf-with-variable-format-string/3805/10).

You could define your function in the `startup.jl` file.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 12, 2023, 6:21pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/5 "2023-12-12T18:21:49Z")

</div>

Related discussions:

- [How to display Float64 with more digits](https://discourse.julialang.org/t/how-to-display-float64-with-more-digits/5628)
- [How to print a number to X digits?](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526)
- [julia - Jula REPL number formatting - Stack Overflow](https://stackoverflow.com/questions/24580334/jula-repl-number-formatting)
- [function to change numeric output format · Issue #6493 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/6493)

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 12, 2023, 6:43pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/6 "2023-12-12T18:43:06Z")

</div>

But why doesn’t the language itself provide a solution for this? Users interest is not lacking.

And what do these errors mean?

```julia
julia> function format(n) Base.show(io::IO, f::Float64) = Printf.format(io, "%.nf", f) end
ERROR: syntax: Global method definition around REPL[9]:1 needs to be placed at the top level, or use "eval".

```

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [December 12, 2023, 6:45pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/7 "2023-12-12T18:45:01Z")

</div>

You might also have a look at [18228](https://discourse.julialang.org/t/what-is-in-your-startup-jl/18228/10)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 12, 2023, 7:44pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/8 "2023-12-12T19:44:31Z")

</div>

> [@joa-quim](#):
>
> And what do these errors mean?

You can’t overload a global function (e.g. `Base.show`) inside another function, without using `eval`.

Note that overloading `Base.show` like this is generally a bad idea — it will effect _everything_, not just the REPL. (e.g. code doing I/O to files, over the network, … anything directly or indirectly using `show`.) [Type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy) is not composable.

> [@joa-quim](#):
>
> But why doesn’t the language itself provide a solution for this?

This is more of a REPL issue than a language issue, I would say.

Implementing something both flexible and composable requires a bit of care, but is do-able by someone who understands Julia’s I/O and `IOContext` architecture, I think. It’s not a 15-minute job, though, and someone has to volunteer do the work.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [December 12, 2023, 9:05pm UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/9 "2023-12-12T21:05:05Z")

</div>

@MA_Laforge has discussed these issues in NumericIO.jl.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 15, 2023, 3:30am UTC](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501/10 "2023-12-15T03:30:26Z")

</div>

> [@stevengj](#):
>
> Implementing something both flexible and composable requires a bit of care, but is do-able by someone who understands Julia’s I/O and `IOContext` architecture, I think. It’s not a 15-minute job, though, and someone has to volunteer do the work.

I volunteered. 😉 A possible PR to provide a simple interface for changing the display precision is:

> <https://github.com/JuliaLang/julia/pull/52543>
>
> Closes #6493: gives a way to control the REPL display precision (the number of s…ignificant digits displayed for floating-point values).
> 
> This is a very common request from users. e.g. on discourse see \[here\](https://discourse.julialang.org/t/how-do-i-restrict-the-number-of-digits-in-the-repl-display/107501) and \[here\](https://discourse.julialang.org/t/scientific-notation-in-repl/79841) and \[here\](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526) and \[here\](https://discourse.julialang.org/t/how-to-display-float64-with-more-digits/5628) and \[here\](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561). A lot of the suggested solutions involve redefining \`show(::IO, ::Float64)\`, which is type piracy and non-composable.
> 
> I wanted a solution which:
> \* Only affects \`display\` (what the REPL uses to show results) and not I/O to any other stream (files, etcetera).
> \* Does not affect performance to any non-\`display\` I/O.
> \* Can be configured independent of the REPL (e.g. the same configuration can be used for IJulia etc), and works in both \`startup.jl\` and a running REPL (or IJulia etc) session. \*But\* if you somehow have multiple REPLs running in parallel from the same Julia process, they can still change their display formats independently.
> \* Is extensible to floating-point types in other packages (opt-in).
> 
> This PR adds two new functions exported by the \`InteractiveUtils\` stdlib (which is loaded in REPL and IJulia sessions):
> \`\`\`jl
> set\_display\_digits!(\[display\], \[T=AbstractFloat\], digits; compact=false)
> unset\_display\_digits!(\[display\], \[T=AbstractFloat\]; compact=false)
> \`\`\`
> to set the displayed precision (corresponding to a format string \`"%.$(digits)g"\`) for the type \`T\` (defaulting to \`AbstractFloat\`, i.e. all supported floating-point types). If you don't pass a \`display\` argument, it affects all currently loaded \`AbstractDisplay\`s (that support this settting) as well as a dictionary of defaults that is used for subsequently created REPL sessions (or IJulia sessions, in the future). By default, it affects the digits shown for all displays, but you can separately control the precision shown in \`:compact=\>true\` \`IOContext\`s by passing the \`compact=true\` argument.
> 
> Internally, a supporting display (currently just \`REPLDisplay\`, but IJulia can opt-in to this later) implements this by wrapping its \`IO\` stream in an \`InteractiveUtils.digitsio(io)\` stream, which overloads \`show\` for the built-in \`AbstractFloat\` types (and external packages can opt-in for their own \`AbstractFloat\` types in the future). This way it has zero effect on any other \`IO\` stream. (\`IOContext\` wrappers around a \`DigitsIO\` stream inherit its floating-point display too.)
> 
> For example:
> \`\`\`jl
> julia\> z = rand(ComplexF64)
> 0.7230118898045041 + 0.33728384864961525im
> 
> julia\> set\_display\_digits!(8)
> 
> julia\> z
> 0.72301189 + 0.33728385im
> 
> julia\> A = rand(2,2) # compact display uses 8 digits too
> 2×2 Matrix{Float64}:
> 0.34100692 0.21678433
> 0.23189211 0.68572311
> 
> julia\> set\_display\_digits!(2, compact=true)
> 
> julia\> A # compact display now uses 2 digits
> 2×2 Matrix{Float64}:
> 0.34 0.22
> 0.23 0.69
> 
> julia\> z # non-compact display still uses 8 digits
> 0.72301189 + 0.33728385im
> \`\`\`
> 
> Before I do any more work on this, however, I want to get some feedback on the overall design. To do:
> \- \[\] Documentation
> \- \[\] Tests
> 
> There is also the question of whether we only want to control digits (always format as \`%.\*g\`) or if we want a configurable format string (con: more complex; pro: more options, but is it actually useful in practice?). However, configurable format strings could be added later on if desired — you'd still want a simple API where you just specify the precision, as well as a more advanced API where you pass a format string.
