# Formatting floating point values as fixed width for printing with "$(value)"?

**URL:** <https://discourse.julialang.org/t/formatting-floating-point-values-as-fixed-width-for-printing-with-value/123968>\
**Category:** New to Julia\
**Tags:** question, io, formatting\
**Created:** [December 18, 2024, 6:03pm UTC](https://discourse.julialang.org/t/formatting-floating-point-values-as-fixed-width-for-printing-with-value/123968 "2024-12-18T18:03:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [December 18, 2024, 6:03pm UTC](https://discourse.julialang.org/t/formatting-floating-point-values-as-fixed-width-for-printing-with-value/123968/1 "2024-12-18T18:03:57Z")

</div>

I’m trying to add some logging to an existing codebase.

I am trying to add some timing information to these logs.

Logs are quite simple, they are just `println` statements wrapped by some functions.

I would like to be able to print floating point values in a fixed width format, so that many lines of logs are relatively easy to read.

Is this possible?

```julia
println("$(datetimenow), $(interval), $(logtext))

```

Here, `interval` is a floating point value with some timing information.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 18, 2024, 6:05pm UTC](https://discourse.julialang.org/t/formatting-floating-point-values-as-fixed-width-for-printing-with-value/123968/2 "2024-12-18T18:05:14Z")

</div>

You can use the `Printf` stblib for that:

> **[Printf · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Printf/#Printf.%40printf)**
>
> Documentation for The Julia Language.

---

<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:** [December 18, 2024, 8:29pm UTC](https://discourse.julialang.org/t/formatting-floating-point-values-as-fixed-width-for-printing-with-value/123968/3 "2024-12-18T20:29:49Z")

</div>

Alternatively, it might be possible to achieve something reasonable without a package using Julia’s base functions `round()` and `rpad()`.

For example:

```julia
aux(x, k, n) = rpad(round(x, digits=k), n)

datetimenow, interval, logtext = eachcol(rand(10,3))
k, n = 4, 11
print("$(rpad("datetimenow", n)) $(rpad("interval", n)) $(rpad("logtext", n))")
foreach((x,y,z) -> println("$(aux(x,k,n)) $(aux(y,k,n)) $(aux(z,k,n))"), datetimenow, interval, logtext)

# result:
datetimenow interval logtext    
0.4307 0.6669 0.417      
0.2751 0.137 0.8011
0.438 0.1341 0.2163
0.7719 0.6215 0.9263
0.0244 0.6474 0.5079
0.6669 0.2355 0.1
0.2717 0.5071 0.1892
0.8993 0.9825 0.9196
0.419 0.3236 0.2753
0.2838 0.3741 0.1114

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 19, 2024, 1:23am UTC](https://discourse.julialang.org/t/formatting-floating-point-values-as-fixed-width-for-printing-with-value/123968/4 "2024-12-19T01:23:37Z")

</div>

PyFormattedStrings.jl provides Python f-string format in Julia, quite convenient for this purpose:

```julia
julia> x = 123.4567

julia> y = "abc"

julia> f"{x:<10.2f} | {y:8s}"
"123.46 | abc"

```
