# Is it possible for Julia to print numeric output in the REPL with underscores as digit separators?

**URL:** <https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962>\
**Category:** General Usage\
**Tags:** numbers, repl\
**Created:** [August 14, 2020, 9:54pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962 "2020-08-14T21:54:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![philip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/philip/32/17191_2.png) [@philip](https://discourse.julialang.org/u/philip)\
**Post date:** [August 14, 2020, 9:54pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/1 "2020-08-14T21:54:52Z")

</div>

Say I need to compute the size of a cross join before attempting it.

My eyes aren’t great.

```julia
julia> 1_000*144*30*20*15*18
23328000000

```

Wait, how many 0s are there?

```julia
julia> ans==2_332_800_000
false

```

Urgh.

Can I just see the output with the \_ in the first place? Then I could also nicely copy and paste it into future computations.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [August 14, 2020, 11:03pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/2 "2020-08-14T23:03:30Z")

</div>

[JuliaHub](https://juliahub.com/ui/Packages/Humanize/MFKuW/1.0.0) perhaps?

---

<div class="post-metadata">

**Author:** ![onButtonUp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/onbuttonup/32/3098_2.png) [@onButtonUp](https://discourse.julialang.org/u/onButtonUp)\
**Post date:** [August 14, 2020, 11:41pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/3 "2020-08-14T23:41:44Z")

</div>

Yes, that works like this . . . It does turn the result as a string . . . But one can cut and paste it as a number easy enough . . .

```julia
Julia> import Humanize: digitsep
Julia> useHyphen(aNumber) = digitsep(aNumber, seperator= "_", per_separator=3)
Julia> 1_000*144*30*20*15*18
23328000000

Julia> useHyphen(ans)
"23_328_000_000"

```

and . . .

```julia
julia> noHyphen(aString) = parse(Int, replace(aString, "_" => ""))
noHyphen (generic function with 1 method)

julia> 1_000*144*30*20*15*18
23328000000

julia> useHyphen(ans)
"23_328_000_000"

julia> noHyphen(ans)
23328000000

```

---

<div class="post-metadata">

**Author:** ![philip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/philip/32/17191_2.png) [@philip](https://discourse.julialang.org/u/philip)\
**Post date:** [August 15, 2020, 12:00am UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/4 "2020-08-15T00:00:23Z")

</div>

Very cool—thanks!

I would think though that there would be some way of overloading the print method used by the REPL to do this without rendering numbers as strings? This is beyond my comprehension but just a hunch so thought I would throw it out in case someone knows how. (I also realize doing so might introduce who knows what sort of downstream problems…)

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [August 15, 2020, 12:19am UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/5 "2020-08-15T00:19:37Z")

</div>

> [@philip](#):
>
> I would think though that there would be some way of overloading the print method used by the REPL to do this without rendering numbers as strings? This is beyond my comprehension but just a hunch so thought I would throw it out in case someone knows how. (I also realize doing so might introduce who knows what sort of downstream problems…)

I tried this a few times by redefining `Base.show` for `::Int`, and I broke the REPL every single time.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 15, 2020, 1:14am UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/6 "2020-08-15T01:14:19Z")

</div>

You can either define a `print_num` function that calls `print` on the result from `Humanize`, or define a new type and extend `Base.show` on it.

---

<div class="post-metadata">

**Author:** ![philip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/philip/32/17191_2.png) [@philip](https://discourse.julialang.org/u/philip)\
**Post date:** [August 15, 2020, 1:21am UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/7 "2020-08-15T01:21:41Z")

</div>

😂 fair enough

---

<div class="post-metadata">

**Author:** ![philip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/philip/32/17191_2.png) [@philip](https://discourse.julialang.org/u/philip)\
**Post date:** [August 15, 2020, 1:36am UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/8 "2020-08-15T01:36:51Z")

</div>

Awesome. So (I believe w/o breaking REPL), combining @cormullion, @onButtonUp, @dpsanders:

```julia
struct Showy
    value
end
import Humanize: digitsep
import Base.show
Base.show(io::IO,x::Showy) = 
    print(io,digitsep(x.value;seperator="_",per_separator=3)) #sic (argument spelling)

```

```julia
julia> 1_000*144*30*20*15*18 |> Showy
23_328_000_000

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 15, 2020, 6:42am UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/9 "2020-08-15T06:42:44Z")

</div>

Note that this should be `print(io,...) `

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [August 15, 2020, 10:42am UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/10 "2020-08-15T10:42:55Z")

</div>

For anyone wondering:

 ![Annotation 2020-08-15 124047](https://global.discourse-cdn.com/julialang/original/3X/c/7/c70bee309318d4462881da2ef3504a1a2785db2a.png)

`julia>` gets very buggy if you press backspace, `]`, `?` etc.

---

<div class="post-metadata">

**Author:** ![philip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/philip/32/17191_2.png) [@philip](https://discourse.julialang.org/u/philip)\
**Post date:** [August 15, 2020, 12:05pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/11 "2020-08-15T12:05:18Z")

</div>

Edited. TY

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 15, 2020, 12:06pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/12 "2020-08-15T12:06:29Z")

</div>

You probably only want to change `show` for `text/plain` output, because internal code may rely on the output of `show(::IO, ::Int)`. This works for me:

```julia
julia> using Humanize: digitsep

julia> Base.show(io::IO, ::MIME"text/plain", x::Int) = print(io, digitsep(x, seperator='_'))

julia> 123456789
123_456_789

```

Might be worth filing an issue on GitHub to make this the default printing in the REPL.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 15, 2020, 12:18pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/13 "2020-08-15T12:18:35Z")

</div>

> [@simeonschaub](#):
>
> make this the default printing in the REPL

Please don’t, I am not sure it is universally preferred.

---

<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:** [August 15, 2020, 12:21pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/14 "2020-08-15T12:21:00Z")

</div>

It would be nice for it to be configurable.

If the behavior demonstrated by @simeonschaub is robust, though, it’s easy to just put it in your startup.jl.

---

<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:** [August 15, 2020, 12:25pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/15 "2020-08-15T12:25:22Z")

</div>

> [@Tamas\_Papp](#):
>
> > [@simeonschaub](#):
> >
> > make this the default printing in the REPL
> 
> Please don’t, I am not sure it is universally preferred.

second this. I like the idea but don’t want to have it by default.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 15, 2020, 12:38pm UTC](https://discourse.julialang.org/t/is-it-possible-for-julia-to-print-numeric-output-in-the-repl-with-underscores-as-digit-separators/44962/16 "2020-08-15T12:38:59Z")

</div>

I don’t really have a dog in this fight, as I don’t deal with large integers like these very often, but it seems sensible enough to me, to warrant discussing this further and GitHub issues are usually a better fit for these kinds of discussion than Discourse.
