# How to print a vector with only 2 decimal places

**URL:** <https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368>\
**Category:** New to Julia\
**Tags:** formatting\
**Created:** [August 30, 2021, 4:17pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368 "2021-08-30T16:17:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 30, 2021, 4:17pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/1 "2021-08-30T16:17:39Z")

</div>

I’m trying to print a vector x with 2 decimal places, but this:  
@sprintf("%.2f ",x)  
gives an error:  
**ERROR:** MethodError: no method matching Float64(::Matrix{Float64})

Is there a way to apply “%.2f” to an entire vector or matrix?

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [August 30, 2021, 4:23pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/2 "2021-08-30T16:23:13Z")

</div>

Two options include:

```julia
julia> using Printf

julia> x = rand(4);

julia> Printf.format.(Ref(Printf.Format("%.2f")), x)
4-element Vector{String}:
 "0.81"
 "0.67"
 "0.31"
 "0.11"

julia> string.(round.(x; digits=2))
4-element Vector{String}:
 "0.81"
 "0.67"
 "0.31"
 "0.11"

```

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 30, 2021, 4:23pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/3 "2021-08-30T16:23:50Z")

</div>

Many thanks!

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 30, 2021, 4:27pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/4 "2021-08-30T16:27:28Z")

</div>

But I don’t want to print strings, I want the actual value: [0.81,0.67, etc]

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 30, 2021, 4:28pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/5 "2021-08-30T16:28:20Z")

</div>

just round then

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 30, 2021, 4:29pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/6 "2021-08-30T16:29:24Z")

</div>

Won’t round the actual number? I want to keep the 64-bit number, just print only 2 decimal places.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 30, 2021, 4:30pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/7 "2021-08-30T16:30:06Z")

</div>

> I’m trying to print a vector x with 2 decimal places

> But I don’t want to print strings,

> just print only 2 decimal places.

I’m very confused. The number is still 64-bit after rounding.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 30, 2021, 4:32pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/8 "2021-08-30T16:32:14Z")

</div>

They want to _print_, not generate strings.

`format.(Ref(stdout), Ref(fmt), x)` will print them all.

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [August 30, 2021, 4:33pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/9 "2021-08-30T16:33:40Z")

</div>

`round.(x; digits=2)` will not change the values in `x`, but create a new array.

Is the question about printing (e.g. to `stdout`) or the display of the numbers (i.e. in the REPL)?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 4:34pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/10 "2021-08-30T16:34:59Z")

</div>

> [@gustaphe](#):
>
> `format.(Ref(stdout), Ref(fmt), x)` will print them all.

```julia
julia> format.(Ref(stdout), Ref(fmt), x)
ERROR: UndefVarError: format not defined

```

Package?

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 30, 2021, 4:35pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/11 "2021-08-30T16:35:15Z")

</div>

**julia\>** x = rand(2)  
2-element Vector{Float64}:  
0.8864506178406879  
0.010471246659258249

**julia\>** print(x)  
[0.8864506178406879, 0.010471246659258249]

I want to print [0.88, 0.01]

Is that less confusing?

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 30, 2021, 4:36pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/12 "2021-08-30T16:36:09Z")

</div>

That should do it, thanks! I misunderstood round.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 30, 2021, 4:36pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/13 "2021-08-30T16:36:33Z")

</div>

```julia
myprint(x) = print(round.(x; sigdigits=2))

```

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 30, 2021, 4:40pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/14 "2021-08-30T16:40:05Z")

</div>

That’s the solution, thanks!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 30, 2021, 5:04pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/16 "2021-08-30T17:04:05Z")

</div>

Same as in the previous suggestion, `Printf`.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 5:06pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/17 "2021-08-30T17:06:28Z")

</div>

Strange:

```julia
julia> using Printf

julia> x=[1.1231233, 2.324234234]
2-element Vector{Float64}:
 1.1231233
 2.324234234

julia> fmt="%.2f"
"%.2f"

julia> format.(Ref(stdout), Ref(fmt), x)
ERROR: UndefVarError: format not defined
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

```

perhaps a version thing? I am still on Julia 1.6.1.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 5:07pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/18 "2021-08-30T17:07:34Z")

</div>

And:

```julia
julia> Printf.format.(Ref(stdout), Ref(fmt), x)
ERROR: MethodError: no method matching format(::Base.TTY, ::String, ::Float64)

```

But:

```julia
julia> Printf.format.(Ref(Printf.Format("%.2f")), x)
2-element Vector{String}:
 "1.12"
 "2.32"

```

---

<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:** [August 30, 2021, 5:44pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/19 "2021-08-30T17:44:52Z")

</div>

As easy as:

```julia
map(x->@printf("%.2f\n",x), rand(4))
0.64
0.18
0.16
0.94

```

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [August 30, 2021, 6:13pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/20 "2021-08-30T18:13:13Z")

</div>

You missed: `Printf.format.(Ref(stdout), Ref(Printf.Format("%.2f")), x)`.

`Printf.format` requires a `Printf.Format` object, and it also isn’t exported (unclear why, since it’s the only Printf function I’m aware of that accepts a string variable instead of a string literal). The main Printf interfaces (e.g. `@printf` and `@sprintf`) only accept string literals for the formatting string.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 6:20pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/21 "2021-08-30T18:20:06Z")

</div>

Something really has been borged:

```julia
julia> using Printf

julia> x=[1.1231233, 2.324234234]
2-element Vector{Float64}:
 1.1231233
 2.324234234

julia> Printf.format.(Ref(stdout), Ref(Printf.Format("%.2f")), x)
1.122.322-element Vector{Nothing}:
 nothing
 nothing

```

But I like that trick:

```julia
julia> length(Printf.format.(Ref(stdout), Ref(Printf.Format("%.2f")), x))
1.122.322

```

But it’s not me, who missed something. I just can’t get it to work. Well, perhaps it isn’t as much important, because I feel, there are better (easier to read) solutions around in this thread.

[Next page](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368.md?page=2)
