How to print a vector with only 2 decimal places

That’s working as it should (although maybe not as expected :crazy_face:). 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 nothings.

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

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.

1 Like

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

			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.