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?
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?
Two options include:
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"
Many thanks!
But I don’t want to print strings, I want the actual value: [0.81,0.67, etc]
just round then
Won’t round the actual number? I want to keep the 64-bit number, just print only 2 decimal places.
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.
They want to print, not generate strings.
format.(Ref(stdout), Ref(fmt), x)
will print them all.
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)?
julia> format.(Ref(stdout), Ref(fmt), x)
ERROR: UndefVarError: format not defined
Package?
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?
That should do it, thanks! I misunderstood round.
myprint(x) = print(round.(x; sigdigits=2))
That’s the solution, thanks!
Same as in the previous suggestion, Printf
.
Strange:
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.
And:
julia> Printf.format.(Ref(stdout), Ref(fmt), x)
ERROR: MethodError: no method matching format(::Base.TTY, ::String, ::Float64)
But:
julia> Printf.format.(Ref(Printf.Format("%.2f")), x)
2-element Vector{String}:
"1.12"
"2.32"
As easy as:
map(x->@printf("%.2f\n",x), rand(4))
0.64
0.18
0.16
0.94
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.
Something really has been borged:
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> 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.