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

<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:32pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/22 "2021-08-30T18:32:31Z")

</div>

That’s working as it should (although maybe not as expected 🤪). The return value of `Printf.format` is `nothing`. It’s printing the values to stdout (no new lines, since there aren’t any in the `Format` string), and then the REPL is printing the results of the broadcasted `format`: a Vector of 2 `nothing`s.

Example with return values suppressed, and a newline added for clarity:

```julia
julia> Printf.format.(Ref(stdout), Ref(Printf.Format("%.2f\n")), x);
0.64
0.06

```

I completely agree that there are possibly better/more ergonomic solutions, depending on the needs. But if a string literal cannot be used (preventing the use of the printf macros), then `Printf.format` seems like the best option for that case.

---

<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, 6:55pm UTC](https://discourse.julialang.org/t/how-to-print-a-vector-with-only-2-decimal-places/67368/23 "2021-08-30T18:55:51Z")

</div>

Thanks for the continued responses, but this works for me:

```julia-auto
			if isa(x,Float64) || # only print 2 decimals
			   isa(x,Vector) ||
			   isa(x,Matrix)	
				p = p * string(round.(x;digits=2)) * " "
			else	
				p = p * string(x) * " "
			end

```

I’m building a string, p, to prompt the user by showing the current stack, like this:

## jacl version 0.1

S: 2 3 \*  
S[6]: 1 2 rand  
S[6 [0.08; 0.68]]: …waiting for input…

jacl uses postfix syntax, obviously.

For anyone interested, send me an email address and I’ll send you the source and doc files.  
I’m a C programmer and would appreciate suggestions for using a more Julia-like style.

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