# Printf with variable number of digits

**URL:** <https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589>\
**Category:** General Usage\
**Tags:** question, formatting\
**Created:** [January 26, 2023, 6:41pm UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589 "2023-01-26T18:41:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maurizio\_Tomasi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maurizio_tomasi/32/384_2.png) [@Maurizio\_Tomasi](https://discourse.julialang.org/u/Maurizio_Tomasi)\
**Post date:** [January 26, 2023, 6:41pm UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589/1 "2023-01-26T18:41:34Z")

</div>

I am trying to write some code that computes some number up to some precision and prints it with the correct number of digits. I tried the following:

```julia
using Printf

function print_number(x, err)
    numofdigits = ceil(Int, -log10(err))
    @printf("%.*f ± %.*f\n", numofdigits, x, numofdigits, err)
end

print_number(3.12345, 0.01) # Should print 3.12 ± 0.01

```

but the code produces the error `ArgumentError: invalid format string: '%.*f ± %.f', invalid type specifier: '*'`.

I wasn’t expecting this, as the [`@printf` documentation](https://docs.julialang.org/en/v1/stdlib/Printf/) explicitly points to the [C++ specification](https://cplusplus.com/reference/cstdio/printf/) of format strings, where the `%.*f` syntax is supported. Am I missing something, or it is not possible with `@printf` to do this? And if it is not, what would be a simple workaround?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 26, 2023, 6:47pm UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589/2 "2023-01-26T18:47:29Z")

</div>

Don’t use `@printf`, use `format` + `Format`:

```julia
julia> using Printf

julia> function print_number(x, err)
           numofdigits = ceil(Int, -log10(err))
           Printf.format(Printf.Format("%.$(numofdigits)f ± %.$(numofdigits)f\n"), x, err)
       end
print_number (generic function with 1 method)

julia> print_number(3.12345, 0.01) # Should print 3.12 ± 0.01
"3.12 ± 0.01\n"

```

---

<div class="post-metadata">

**Author:** ![Maurizio\_Tomasi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maurizio_tomasi/32/384_2.png) [@Maurizio\_Tomasi](https://discourse.julialang.org/u/Maurizio_Tomasi)\
**Post date:** [January 26, 2023, 6:52pm UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589/3 "2023-01-26T18:52:56Z")

</div>

Thanks! So I understand that the `*` character is not supported in Julia. Do you think this should be stated somewhere in the documentation?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 26, 2023, 7:03pm UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589/4 "2023-01-26T19:03:52Z")

</div>

I’m not familiar with the internals of the Printf module, but it’d appear it doesn’t work, so yes, it may be worth mentioning it in the documentation.

---

<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:** [January 26, 2023, 7:20pm UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589/5 "2023-01-26T19:20:53Z")

</div>

Linking [similar post](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685) and [PR](https://github.com/JuliaLang/julia/pull/40105#issue-835945208).

---

<div class="post-metadata">

**Author:** ![Maurizio\_Tomasi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maurizio_tomasi/32/384_2.png) [@Maurizio\_Tomasi](https://discourse.julialang.org/u/Maurizio_Tomasi)\
**Post date:** [January 27, 2023, 8:37am UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589/6 "2023-01-27T08:37:23Z")

</div>

Thanks! I looked for existing threads in this forum but wasn’t able to find that one. (I could just have looked in the issues of the `Printf` package, silly of me…)

---

<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:** [January 27, 2023, 10:18am UTC](https://discourse.julialang.org/t/printf-with-variable-number-of-digits/93589/7 "2023-01-27T10:18:19Z")

</div>

> [@Maurizio\_Tomasi](#):
>
> Do you think this should be stated somewhere in the documentation?

> [@giordano](#):
>
> so yes, it may be worth mentioning it in the documentation.

Most flags that have some dynamic interpretation aren’t supported (we do some length checking to make sure the format object & the number of arguments match for example, to prevent [security issues](https://owasp.org/www-community/attacks/Format_string_attack)). The general rule of thumb is that if the formatting can be done purely based on a constant format string, it should work. This should definitely be better documented though, instead of just linking to an external ressource.
