and then it prints the solution m to the problem–as directed by the @show value(m) line. But it prints many decimal places of m. I only want 2 or 3 decimal places. How do I get it to give me only the desired number of decimal places?
I know I can use @show value(round(m,digits=3)) but that is annoying to write when I want to print out many variables in the solution since then I have to write the round function many times. Is there a way to just impose the rounding automatically for all variables in the solution?
You may want to save all your values in a data structure as @Henrique_Becker suggested, but to answer your question directly, you can write your own macro:
julia> macro show2(x)
quote
print($(QuoteNode(x)))
print(" = ")
println(round($x; digits=2))
$x
end
end
@show2 (macro with 1 method)
julia> @show sqrt(3);
sqrt(3) = 1.7320508075688772
julia> @show2 sqrt(3);
sqrt(3) = 1.73
That’s great thanks! But how do out save all values to a data structure? I see in the docs it talks about containers
so I could do that, but then I think I would have to rewrite the model functions in terms of a vector (i.e. a container) for the variables.
You can query a vector of all variables with all_variables(model), but this doesn’t preserve the container structure. You’ll need to write something custom.