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
23328000000

Wait, how many 0s are there?

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.

2 Likes

JuliaHub 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
23328000000

Julia> useHyphen(ans)
"23_328_000_000"

and . . .

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

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 Base.show 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 Base.show on it.

:joy: fair enough

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

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> 1_000*144*30*20*15*18 |> Showy
23_328_000_000
3 Likes

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

4 Likes

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

3 Likes

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