# Displaying numbers in Julia

**URL:** <https://discourse.julialang.org/t/displaying-numbers-in-julia/112807>\
**Category:** General Usage\
**Tags:** numbers, formatting, printf\
**Created:** [April 10, 2024, 10:20pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807 "2024-04-10T22:20:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [April 10, 2024, 10:20pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/1 "2024-04-10T22:20:50Z")

</div>

I want to make a very simple calculation:

```Julia
julia> 6500 * ( (1.10)^58 )
1.6356451710902702e6

```

But, the format I’m hoping to for is `1,635,645.17 ` or `1635645.17`.

I spent 10 minutes searching the internet & Julia built in help & couldn’t find an easy way to display something that seems so simple.

Please tell me I’m missing something obvious.

Summary of some proposed solutions:

```Julia
julia> x = 6500 * ( (1.10)^58 );

julia> using Printf; @printf("%.2f", x)
1635645.17
julia> using Format; format(x; commas=true, precision=2)
"1,635,645.17"

```

I’m surprised `round()` doesn’t work here.

I’m concerned that something so simple isn’t automatically possible in Julia w/o calling other packages…

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 10, 2024, 10:33pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/2 "2024-04-10T22:33:45Z")

</div>

This fine?

```julia-repl
julia> using Printf

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

julia> 6500 * ( (1.10)^58 )
1635645.171090

```

You can then just update the display as needed.

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [April 10, 2024, 10:35pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/3 "2024-04-10T22:35:21Z")

</div>

I know that `Formatting` is deprecated. But you were able to do it with it.

```julia
using Formatting
x = 2124313412.1231243
new_value = replace(format(x, commas = true), "," => "_")

```

which gives “2\_124\_313\_412.123124”

If you only want “,”:

```julia
new_value = format(x, commas = true)

```

---

<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 10, 2024, 10:38pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/4 "2024-04-10T22:38:09Z")

</div>

Pirating `Base.show` seems too dangerous for me.

Format.jl replaces `Formatting.jl`.

```jl
using Format
julia> format(x; commas=true, precision=2)
"2,124,313,412.12"

```

Also check out [GitHub - ma-laforge/NumericIO.jl: Fine control over numeric output: Scientific/Engineering/SI-notation +UTF8](https://github.com/ma-laforge/NumericIO.jl)

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 10, 2024, 11:13pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/8 "2024-04-10T23:13:58Z")

</div>

Ah yea, I thought you wanted to change the default printing of the REPL. But glad that the general approach seemed to help @Albert_Zevelev ! 😃

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [April 10, 2024, 11:14pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/9 "2024-04-10T23:14:55Z")

</div>

To clarify, is there a way for the @printf macro to display commas?

---

<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 10, 2024, 11:27pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/10 "2024-04-10T23:27:58Z")

</div>

That Printf.jl limitation is solved by Format.jl:

```julia
using Format
s = cfmt("%'.2f", 6500 * (1.10)^58) 
"1,635,645.17"

```

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [April 10, 2024, 11:31pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/11 "2024-04-10T23:31:33Z")

</div>

I happened to have this in one of my codes, you’re welcome to use it.

```julia
function with_commas(num::AbstractFloat, trunc::Integer=3)
    sig = Int(floor(num))
    frac = (num - sig)
    sig_str = string(sig)
    frac_str = string(frac)[2:2+trunc]
    return replace(sig_str, r"(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))" => ",") * frac_str
end

julia> with_commas(1.6356451710902702e6)
"1,635,645.171"

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 10, 2024, 11:36pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/12 "2024-04-10T23:36:23Z")

</div>

> [@rafael.guerra](#):
>
> That [Printf.jl](https://juliahub.com/ui/Packages/General/Printf) limitation

[Support `'` format flag for `@printf` · Issue #29077 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/29077)

This is rooted in C’s printf, and I only did a cursory reading and am uncertain whether the apostrophe flag for a thousands separator is standard or an extension.

---

<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 10, 2024, 11:43pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/13 "2024-04-10T23:43:54Z")

</div>

Just a note: Printf.jl (or Format.jl) rounds the number instead of truncating.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [April 10, 2024, 11:52pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/14 "2024-04-10T23:52:27Z")

</div>

True enough. I didn’t care about that, but if you do:

```julia
function with_commas(num::AbstractFloat, trunc::Integer=3)
    sig = Int(floor(num))
    frac = (num - sig)
    sig_str = string(sig)
    frac_str = string(round(frac, digits=trunc))[2:end]
    return replace(sig_str, r"(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))" => ",") * frac_str
end

julia> with_commas(1.6356451710902702e6)
"1,635,645.171"

julia> with_commas(1.6356451715902702e6)
"1,635,645.172"

```

It’s available…

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 11, 2024, 12:30am UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/15 "2024-04-11T00:30:01Z")

</div>

If going in the direction of reimplementing the string formatting instead of installing a package (yet I would think any good implementation is worth sharing as a package), I think changing the number of digits per delimiter, the delimiter character, and the decimal delimiter character can be low-hanging fruit. On the other hand, formats that vary number of digits per delimiter and the delimiter characters by magnitude could be more simply specified by label if finer control would be too complicated.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [April 11, 2024, 12:57am UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/16 "2024-04-11T00:57:48Z")

</div>

> [@Albert\_Zevelev](#):
>
> To clarify, is there a way for the @printf macro to display commas?

There’s an open PR for this, awaiting review I believe. EDIT: needs a reviewer.

[Support the flag `'` for thousand separator in Printf, issue #29077 by ki-chi · Pull Request #42145 · JuliaLang/julia (github.com)](https://github.com/JuliaLang/julia/pull/42145)

---

<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:** [April 11, 2024, 6:41am UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/17 "2024-04-11T06:41:20Z")

</div>

> [@mnemnion](#):
>
> ```julia
> function with_commas(num::AbstractFloat, trunc::Integer=3)
> sig = Int(floor(num))
> frac = (num - sig)
> sig_str = string(sig)
> frac_str = string(round(frac, digits=trunc))[2:end]
> return replace(sig_str, r"(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))" => ",") * frac_str
> end
> 
> ```

The `floor` call leads to some possibly undesirable behaviour:

```julia
julia> x = 1.6356451710902702e6
1.6356451710902702e6

julia> with_commas(x)
"1,635,645.171"

julia> with_commas(-x)
"-1,635,646.829"

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [April 11, 2024, 10:07pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/18 "2024-04-11T22:07:16Z")

</div>

The `floor` call leads to some possibly undesirable behaviour:

```julia
julia> x = 1.6356451710902702e6
1.6356451710902702e6

julia> with_commas(x)
"1,635,645.171"

julia> with_commas(-x)
"-1,635,646.829"

```

isn’t the solution to check if the number is negative, if so mark a flag then turn the number positive and perform the function then after the function is done add a negative sign to the front of the string if the number is negative?

```julia
function with_commas(num::AbstractFloat, trunc::Integer=3)
    negflag = false
    if num < 0
        negflag = true
        num = -1 * num
    end
    sig = Int(floor(num))
    frac = (num - sig)
    sig_str = string(sig)
    frac_str = string(round(frac, digits=trunc))[2:end]
    newstring = replace(sig_str, r"(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))" => ",") * frac_str
    if negflag == true
        newstring = "-" * newstring
    end
    return newstring
end

```

```julia
julia> x = 1.6356451710902702e6
1.6356451710902702e6

julia> with_commas(x)
"1,635,645.171"

julia> with_commas(-x)
"-1,635,645.171"

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 11, 2024, 11:04pm UTC](https://discourse.julialang.org/t/displaying-numbers-in-julia/112807/19 "2024-04-11T23:04:27Z")

</div>

This isn’t optimized for speed (too many allocations…) but it may serve as another base for further optimization and parameters such as different bases, and different separators etc:

```julia
function with_commas2(num::AbstractFloat, trunc::Integer=3)
    d,r = divrem(num, 1000)
    s = string(round(r; digits=trunc))
    while d > 0
        d,r = divrem(d, 1000)
        s = string(floor(Int, r))*','*s
    end
    s
end

```

Also, perhaps `trunc` → `digits` to match `round` would be nicer.  
On `x` in the post it is 2x faster.
