# 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:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [November 21, 2020, 7:27am UTC](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/1 "2020-11-21T07:27:49Z")

</div>

Hello!

Suppose I have the number, “1”, how would I make it so that it would always be printed as “1.000”?

Kind regards

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 21, 2020, 8:27am UTC](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/2 "2020-11-21T08:27:16Z")

</div>

```julia
julia> using Printf

julia> a = 1;

julia> @printf("%.3f\n", a)
1.000

```

There’s also `@sprintf` to get a string value, see [Printf · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Printf/) for details.

---

<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

```
