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

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

My eyes aren’t great.

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

Wait, how many 0s are there?

julia> ans==2_332_800_000


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

1 Like perhaps?

1 Like

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> import Humanize: digitsep
Julia> useHyphen(aNumber) = digitsep(aNumber, seperator= "_", per_separator=3)
Julia> 1_000*144*30*20*15*18

Julia> useHyphen(ans)

and . . .

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

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

julia> useHyphen(ans)

julia> noHyphen(ans)

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…)

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

1 Like

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

:joy: fair enough

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

struct Showy
import Humanize: digitsep
import,x::Showy)  = 
    print(io,digitsep(x.value;seperator="_",per_separator=3)) #sic (argument spelling)
julia> 1_000*144*30*20*15*18 |> Showy

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

1 Like

For anyone wondering:

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

Edited. TY

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> using Humanize: digitsep

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

julia> 123456789

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


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


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.

1 Like

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

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.

1 Like