# Formatted printing of arrays

**URL:** <https://discourse.julialang.org/t/formatted-printing-of-arrays/3121>\
**Category:** General Usage\
**Created:** [April 8, 2017, 5:34pm UTC](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121 "2017-04-08T17:34:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![asubedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asubedi/32/18651_2.png) [@asubedi](https://discourse.julialang.org/u/asubedi)\
**Post date:** [April 8, 2017, 5:34pm UTC](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121/1 "2017-04-08T17:34:37Z")

</div>

I want to print the elements of an array of floating point numbers in the same format. In Python/Numpy, I do:

np.set\_printoptions(formatter={‘float’: ‘{: 10.6f}’.format, ‘complexfloat’: ‘{: 10.6f}’.format})

and the simple command “print someArray” will print the elements of an array in a uniform format.

For example, I want arr = [1.0 2.583] to be printed as [1.000 2.583] rather than [1.0 2.583].

Is it possible to do this in Julia?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 9, 2017, 8:47am UTC](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121/2 "2017-04-09T08:47:40Z")

</div>

In theory you can do something like this:

```julia
julia> a = randn(4, 4)
4×4 Array{Float64,2}:
 -1.93222 -0.427925 -0.502227 0.522741
  0.240474 -1.13067 1.68355 0.839821
  1.79882 0.918598 1.28506 -1.38715 
  0.0540847 0.197535 -0.204077 -0.282407

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

julia> a
4×4 Array{Float64,2}:
 -1.932 -0.428 -0.502 0.523
  0.240 -1.131 1.684 0.840
  1.799 0.919 1.285 -1.387
  0.054 0.198 -0.204 -0.282

```

but I don’t know whether it’s recommended, or indeed how to undo it… 🙂

---

<div class="post-metadata">

**Author:** ![asubedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asubedi/32/18651_2.png) [@asubedi](https://discourse.julialang.org/u/asubedi)\
**Post date:** [April 10, 2017, 12:45am UTC](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121/3 "2017-04-10T00:45:34Z")

</div>

This is what I was looking for.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 10, 2017, 7:32am UTC](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121/4 "2017-04-10T07:32:56Z")

</div>

Cool. Let us know of your experiences! (And choose your format string carefully, that one was just plucked from thin air.😀)

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 10, 2017, 7:38am UTC](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121/5 "2017-04-10T07:38:46Z")

</div>

It’s not recommended to do it this way. See [https://github.com/JuliaLang/julia/issues/20509](https://github.com/JuliaLang/julia/issues/20509) for an issue whose fix can make this much nicer.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 10, 2017, 7:56am UTC](https://discourse.julialang.org/t/formatted-printing-of-arrays/3121/6 "2017-04-10T07:56:39Z")

</div>

Perhaps the [docs](https://docs.julialang.org/en/latest/manual/types.html#Custom-pretty-printing-1) should contain some warnings about using this method on built-in types?
