# How to print a number to X digits?

**URL:** <https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526>\
**Category:** New to Julia\
**Tags:** repl, io\
**Created:** [November 21, 2020, 7:27am UTC](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526 "2020-11-21T07:27:49Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [November 21, 2020, 2:35pm UTC](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/3 "2020-11-21T14:35:23Z")

</div>

You can set it for a whole REPL session instead of for every instance. For example, the following uses 4-digit MATLAB-like formatting:

```julia
julia> using Printf

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

julia> 1.0
1.0000

julia> 2/3
0.6667

julia> rand(5)
5-element Array{Float64,1}:
 0.4112
 0.4889
 0.1706
 0.4589
 0.7526

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526)._
