# Scientific Notation in REPL

**URL:** <https://discourse.julialang.org/t/scientific-notation-in-repl/79841>\
**Category:** New to Julia\
**Tags:** numbers, repl, io\
**Created:** [April 22, 2022, 1:34pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841 "2022-04-22T13:34:06Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)\
**Post date:** [April 22, 2022, 1:34pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/1 "2022-04-22T13:34:06Z")

</div>

Is there a way to temporally change `show()` not to use scientific notation in REPL? or at least print with specific power of n, ex. e3 or e12 ?

I’m not used to using scientific notation and seeing two values in different notation isn’t obvious at glance which value is larger.

```julia
julia> [1000000.0, 10000.0]
2-element Vector{Float64}:
     1.0e6
 10000.0

```

The [documentation says](https://docs.julialang.org/en/v1/stdlib/REPL/#The-Julian-mode) that `show()` is the function to print in REPL. But not sure how to customize it.

Is it possible to show the values in either way?

```julia
julia> [1000000.0, 10000.0]
2-element Vector{Float64}:
1000000.0
  10000.0

```

or

```julia
julia> [1000000.0, 10000.0]
2-element Vector{Float64}:
1000.0e3
  10.0e3

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 22, 2022, 2:15pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/2 "2022-04-22T14:15:10Z")

</div>

Maybe via `IOContext` [I/O and Network · The Julia Language](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext-Tuple%7BIO,%20Pair%7D) ?

Otherwise you need to override a Base.show method.

What would you like it to print?

---

<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:** [April 22, 2022, 4:48pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/3 "2022-04-22T16:48:16Z")

</div>

See one example of customization [in this post](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/3).

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 22, 2022, 10:51pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/4 "2022-04-22T22:51:46Z")

</div>

This doesn’t help with the solution you’re looking for, but for the issue of which one is larger- it’s always the one in scientific notation, because the printing only switches to it for large values.

Edit: at least I think so- I always assumed this is how it works but I’m not sure that’s written anywhere…

---

<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:** [April 22, 2022, 10:53pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/5 "2022-04-22T22:53:15Z")

</div>

[https://github.com/ma-laforge/NumericIO.jl/](https://github.com/ma-laforge/NumericIO.jl/)

---

<div class="post-metadata">

**Author:** ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)\
**Post date:** [April 23, 2022, 10:45am UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/6 "2022-04-23T10:45:55Z")

</div>

Thanks @rafael.guerra, this seems to be the easiest one:

```julia
julia> [1000000.0, 10000.0]
2-element Vector{Float64}:
     1.0e6
 10000.0

julia> using Printf

julia> Base.show(io::IO, f::Float64) = @printf(io, "%.1f", f)

julia> [1000000.0, 10000.0]
2-element Vector{Float64}:
 1000000.0
   10000.0

julia> 

```

---

<div class="post-metadata">

**Author:** ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)\
**Post date:** [April 23, 2022, 10:55am UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/7 "2022-04-23T10:55:07Z")

</div>

it seems to me that NumericIO.jl is a bit behind?

- It’s still using `~/.juliarc.jl`, as it’s been replaced by `~/.julia/config/startup.jl` in Julia 1.0 ([.juliarc.jl in Julia1.0](https://discourse.julialang.org/t/juliarc-jl-in-julia1-0/14354))
- [REPL setup](https://github.com/ma-laforge/NumericIO.jl/#improved-repl-output-julia-console) in the README doesn’t work with Julia 1.7.

```julia
julia> using NumericIO
julia> Base.display(r::Base.REPL.REPLDisplay, v::Union{Float32,Float64}) = print(formatted(Base.REPL.outstream(r.repl), :SI, ndigits=4), v)
ERROR: UndefVarError: REPL not defined
Stacktrace:
 [1] getproperty(x::Module, f::Symbol)
   @ Base ./Base.jl:35
 [2] top-level scope
   @ REPL[7]:1

```

---

<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:** [April 23, 2022, 6:14pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/8 "2022-04-23T18:14:34Z")

</div>

@MA_Laforge might have more to say

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [May 8, 2022, 6:32pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/9 "2022-05-08T18:32:17Z")

</div>

Thanks for pinging me. @jar1

> [@yashi](#):
>
> it seems to me that NumericIO.jl is a bit behind?

I haven’t touched this module in quite a while.  
I’ll try to revisit that example sometime this week. I should definitively at least update to the readme to point to `juliarc.jl` → `startup.jl`.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [May 9, 2022, 2:25am UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/10 "2022-05-09T02:25:49Z")

</div>

> [@yashi](#):
>
> [REPL setup](https://github.com/ma-laforge/NumericIO.jl/#improved-repl-output-julia-console) in the README doesn’t work with Julia 1.7.

Correct: `REPL` moved out of base and into its own module.

You now have to write:

```julia
import REPL
using NumericIO
Base.display(r::REPL.REPLDisplay, v::Union{Float32,Float64}) = print(formatted(REPL.outstream(r.repl), :SI, ndigits=4), v)

```

### Dangers of overwriting `show()`

I recommend against overwriting `show()` as it is somewhat of a base function, and changing it might have unexpected repercussions in other modules elsewhere.

It is much safer to overwrite the `Base.display()` function, as it applies only to results displayed to the REPL (aka Julia “console”). REPL output is meant for human consumption, and is not typically read back by machine.

For more details on the display/print/show/… call stack, you might want to take a look here:

> [@Improving doc for display/print/show/repr/](https://discourse.julialang.org/t/improving-doc-for-display-print-show-repr/69124/3):
>
> Sorry. Got sidetracked when I first outlined this issue - haven’t have much time to look deeper into it. Decided to include graphical view of call stack before I forget I made it (for a second time):

(Note that display calls either `print()` or `show()`… but I did not draw it on that diagram).

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [May 9, 2022, 2:37am UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/11 "2022-05-09T02:37:28Z")

</div>

### Changing `Vector`s/`Array`s are displayed

Unfortunately, Julia will not change how it displays vectors if you overwrite the `Base.display(r::REPL.REPLDisplay, v::Union{Float32,Float64})` method.

That’s because the call tree used to display vectors does not call `display()` on individual elements of the array/vector.

Here is an example of what you _ **could** _ define to customize the `display()` of numeric `Vector`s.

```julia-auto
import REPL
using NumericIO

function Base.display(r::REPL.REPLDisplay, v::Vector)
	N = length(v)
	println(REPL.outstream(r.repl), N, "-element ", typeof(v), ":")
	for v_i in v
		println(formatted(REPL.outstream(r.repl), :SI, ndigits=4), v_i)
	end
end

```

### And what about `Array`s?

"`Array`"s seem to use a more elaborate algorithm to display its multi-dimensional contents while maintaining good readability. You would have to experiment a bit to get your `display(::Array, ...)` function to produce pleasant results.

### Comments

I honestly think we should consider re-evaluating how the Julia call tree `display()`s vectors & arrays so we can more easily switch out how numbers are displayed on the REPL.

What you are requesting sounds fairly reasonable to me - but I’m not convinced the solution is that trivial.

---

<div class="post-metadata">

**Author:** ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)\
**Post date:** [May 9, 2022, 10:53am UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/12 "2022-05-09T10:53:31Z")

</div>

> [@MA\_Laforge](#):
>
> changing it might have unexpected repercussions in other modules elsewhere.

Would you mind elaborating me a bit more? In what case does a module read the string from `show()`? Does it solve if I specify `io` to be `stdout`?

---

<div class="post-metadata">

**Author:** ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)\
**Post date:** [May 9, 2022, 11:03am UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/13 "2022-05-09T11:03:52Z")

</div>

I assume you meant that some functions are relying on `show()` to convert values to string. ex) `repr()`?

> help?\> repr()
> 
> `repr(x; context=nothing)`
> 
> Create a string from any value using the `show` function. You  
> should not add methods to repr; define a show method  
> instead.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [May 10, 2022, 3:24am UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/14 "2022-05-10T03:24:59Z")

</div>

Indeed: `repr()` _is_ an example of unintended consequences - but let me paint a clearer picture…

### More generally:

Overwriting the `show(::IO, ::Float64)` method means you are modifying how pretty much ALL code EVERYWHERE where Julia functions `print()` `Float64` values. This doesn’t just affect the REPL output.

Let’s assume you overwrite show as:

```julia-auto
Base.show(io::IO, f::Float64) = @printf(io, "%.1f", f)

```

That means that if some code somewhere was trying to print out 4.2e-6 to file… then it would end up writing “0.0” instead of “4.2e-6” (The normal behaviour).

Just like that, your one line of code broke pretty much any other function that needed to accurately write out fractional values that are significantly smaller than one to an `::IO` device (like a file).

Maybe that’s ok if you write a tiny little custom program that doesn’t really rely on other functions… Maybe.

That being said: I strongly recommend you don’t do this unless you really just want a quick and dirty solution for your (throw-away) Julia-as-a-line-calculator session.

---

<div class="post-metadata">

**Author:** ![baandersen](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@baandersen](https://discourse.julialang.org/u/baandersen)\
**Post date:** [May 12, 2022, 12:57pm UTC](https://discourse.julialang.org/t/scientific-notation-in-repl/79841/15 "2022-05-12T12:57:42Z")

</div>

I wrote this function for this purpose. I keep it in my startup.jl . If I knew Julia better, I would make this into another dispatch of the @printf macro or, more to the point in this case, the show() function

```julia
""" 
    printfa = print format array
"""
function printfa(A, fmt="%7.3f")
    print("[")
    if ndims(A) == 1
        for i in range(1, stop=length(A))
            @eval @printf($fmt, $A[$i])
            if i != length(A)
                print(", ")
            end
        end
    else
        m, n = size(A)
        for i in range(1, stop=m), j in range(1, stop=n)
            @eval @printf($fmt, $A[$i, $j])
            if j < n
                print(" ")
            elseif j == n && i < m
                print("; \n")
            end
        end
    end
    println("]")

end

```
