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

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?

1 Like

Check this post for a Matlab-like solution.

1 Like

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

function display_digits(n)
    using Printf, f::Float64) = @printf(io, "", f)

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].


To use a variable in the Printf format string, see for example this post.

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

Related discussions:

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

And what do these errors mean?

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

You might also have a look at 18228

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

Note that overloading 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 is not composable.

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.


@MA_Laforge has discussed these issues in NumericIO.jl.

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