# How to format float numbers in human-friendly format?

**URL:** <https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209>\
**Category:** New to Julia\
**Tags:** question, formatting\
**Created:** [March 30, 2021, 9:33am UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209 "2021-03-30T09:33:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matklad/32/17976_2.png) [@matklad](https://discourse.julialang.org/u/matklad)\
**Post date:** [March 30, 2021, 9:33am UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/1 "2021-03-30T09:33:13Z")

</div>

I am writing a short script in Julia, and I need to print a bunch of floating point numbers for the user. If I just `print("my number: $f")`, I get output like `"my number: 2.99999999999"` which is too noisy. I want a human-friendly format. A good approximation would be just using two digits after the point. Is there convenient way to do this? I can use `round(f; digits = 2)`, but that’s a lot of typing. I am looking at something a-la `{f:.2}`.

Here’s the tiny script in question: [config/t at 72a432ffd6bd56db13658d961de9abb740136cb4 · matklad/config · GitHub](https://github.com/matklad/config/blob/72a432ffd6bd56db13658d961de9abb740136cb4/bin/t)

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [March 30, 2021, 10:02am UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/2 "2021-03-30T10:02:03Z")

</div>

I don’t know how “noisy” it is for you, but the command for versatile printing is [`printf`](https://docs.julialang.org/en/v1/stdlib/Printf/), which by the way is a lot faster than before in Julia 1.6

In this case, the command would be:

```julia
@printf "my number: %.2f" f

```

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matklad/32/17976_2.png) [@matklad](https://discourse.julialang.org/u/matklad)\
**Post date:** [March 30, 2021, 10:40am UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/3 "2021-03-30T10:40:39Z")

</div>

Aha, printf indeed works better, thanks!

[https://github.com/matklad/config/commit/bcf7876574b0f1256a74592859125f98cdc7a50e](https://github.com/matklad/config/commit/bcf7876574b0f1256a74592859125f98cdc7a50e)

It’s a bummer that I can’t do both string interpolation and formatted output, but I can live with that.

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [March 30, 2021, 10:46am UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/4 "2021-03-30T10:46:42Z")

</div>

I don’t exactly know what you mean by string interpolation, but there is also a way to print to a string in the very same page, [`sprintf`](https://docs.julialang.org/en/v1/stdlib/Printf/#Printf.@sprintf).

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matklad/32/17976_2.png) [@matklad](https://discourse.julialang.org/u/matklad)\
**Post date:** [March 30, 2021, 10:51am UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/5 "2021-03-30T10:51:54Z")

</div>

There are ways to combine values and string pieces:

- Using string interpolation: `"elapsed: $(f)s"`
- Using sprintf: `@sprintf "number: %.2fs" f`

The first is more convenient, as you don’t have to match format string with n arguments. The second one allows formatting. Ideally, it should be possible to have both. In Python, for example, it is possible to write `f"elapsed: {f:.2}s"`.

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [March 30, 2021, 12:04pm UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/6 "2021-03-30T12:04:32Z")

</div>

Oh, I get it. Nope, it seems there is no alternative for that (that I know of), but I guess it should be relatively easy to make such thing.

If want to retain interpolation but you don’t need as much control over how it is printed, you can always use `IOContext` property `:compact = true` (see examples in [I/O and Network · The Julia Language](https://docs.julialang.org/en/v1/base/io-network/#Base.print))

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 30, 2021, 12:25pm UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/7 "2021-03-30T12:25:22Z")

</div>

The [Formatting](https://github.com/JuliaIO/Formatting.jl) package is an option:

```julia
julia> using Formatting

julia> printx(x,N) = sprintf1("%10.$(N)f",x)
printx (generic function with 1 method)

julia> printx(π,3)
" 3.142"

julia> printx(π,6)
" 3.141593"

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 30, 2021, 5:25pm UTC](https://discourse.julialang.org/t/how-to-format-float-numbers-in-human-friendly-format/58209/8 "2021-03-30T17:25:29Z")

</div>

That issue in particular is relevant: [https://github.com/JuliaIO/Formatting.jl/issues/41](https://github.com/JuliaIO/Formatting.jl/issues/41)

It mentions the [StringLiterals](https://github.com/JuliaString/StringLiterals.jl) package which comes closer to what @matklad is looking for.

None of this has the simplicity of `{f:.2}`… Maybe there’s room for another package 🙂 (or a PR to Formattting.jl?)
