# Printf with custom format (custom data type)

**URL:** <https://discourse.julialang.org/t/printf-with-custom-format-custom-data-type/101782>\
**Category:** General Usage\
**Tags:** io\
**Created:** [July 19, 2023, 1:52pm UTC](https://discourse.julialang.org/t/printf-with-custom-format-custom-data-type/101782 "2023-07-19T13:52:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiroghost](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@shiroghost](https://discourse.julialang.org/u/shiroghost)\
**Post date:** [July 19, 2023, 1:52pm UTC](https://discourse.julialang.org/t/printf-with-custom-format-custom-data-type/101782/1 "2023-07-19T13:52:14Z")

</div>

Hi all,

Is it possible to add format specifiers to printf for custom data types?

Thanks

---

<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:** [July 19, 2023, 2:10pm UTC](https://discourse.julialang.org/t/printf-with-custom-format-custom-data-type/101782/2 "2023-07-19T14:10:40Z")

</div>

Could this work for you:

```julia
using Printf

struct MyType; a::Float64; end

Base.show(io::IO, M::MyType) = @printf(io, "%.2f", M.a)

x = MyType(1.1)

```

or maybe this is better suited:

```julia
using Printf

struct MyType2; a::Float64; ϵ::Float64; end

myfmt = Printf.Format("%.3f +- %.3f")
Printf.format(M::MyType2) = Printf.format.(Ref(myfmt), M.a, M.ϵ)

M = MyType2(1.0, 0.2)
Printf.format(M)

"1.000 +- 0.200"

```

---

<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:** [July 19, 2023, 2:52pm UTC](https://discourse.julialang.org/t/printf-with-custom-format-custom-data-type/101782/3 "2023-07-19T14:52:43Z")

</div>

In general, no - custom format specifiers in format strings are not supported. I’d recommend the `show` approach insted.
