# Rounding solution output in JuMP

**URL:** <https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, rounding\
**Created:** [March 29, 2023, 4:20pm UTC](https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786 "2023-03-29T16:20:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [March 29, 2023, 4:20pm UTC](https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786/1 "2023-03-29T16:20:29Z")

</div>

When I solve a problem in JuMP, my code ends with

```julia
optimize!(model);
    @show value(m);
end

```

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?

Thanks

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 29, 2023, 6:22pm UTC](https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786/2 "2023-03-29T18:22:02Z")

</div>

Not that I know of.

I believe the usual is to save all of them rounded in the same or other data structure. But you need to apply round at least one time.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 29, 2023, 6:37pm UTC](https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786/3 "2023-03-29T18:37:07Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [March 29, 2023, 6:50pm UTC](https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786/4 "2023-03-29T18:50:06Z")

</div>

That’s great thanks! But how do out save all values to a data structure? I see in the docs it talks about [containers](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_JuMP/#tutorial_variable_container)  
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.

Is there something where I could do like

```julia
data_structure_for_solution_vars = optimize!(model).solution # ???
@show2 data_structure_for_solution_vars

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 29, 2023, 7:12pm UTC](https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786/5 "2023-03-29T19:12:52Z")

</div>

I’m not sure what’s idiomatic in JuMP, but you could probably do something like

```julia
round2(x) = round(x; digits=2);
map(round2∘value, (;x,y,z))

```

if your parameters are `x,y,z`.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 29, 2023, 7:32pm UTC](https://discourse.julialang.org/t/rounding-solution-output-in-jump/96786/6 "2023-03-29T19:32:55Z")

</div>

> [@samerb](#):
>
> But how do out save all values to a data structure?

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.
