# Round off decision variable

**URL:** <https://discourse.julialang.org/t/round-off-decision-variable/107988>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [December 23, 2023, 10:10pm UTC](https://discourse.julialang.org/t/round-off-decision-variable/107988 "2023-12-23T22:10:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![quais](https://avatars.discourse-cdn.com/v4/letter/q/ce7236/32.png) [@quais](https://discourse.julialang.org/u/quais)\
**Post date:** [December 23, 2023, 10:10pm UTC](https://discourse.julialang.org/t/round-off-decision-variable/107988/1 "2023-12-23T22:10:37Z")

</div>

Hey, is there a way to round off decision variables? I initialized the following variable, which is given in hours:

```julia
n = 4
model = Model()
@variable(model, start_n[1:n] >= 0, Int)

```

In one of my cost functions, however, I need the value in days, so I have to divide the value by 24 hours and then round down to a whole number.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [December 26, 2023, 10:00am UTC](https://discourse.julialang.org/t/round-off-decision-variable/107988/2 "2023-12-26T10:00:43Z")

</div>

I think you could create a second decision variable, also constrained to be integer, like so:

```julia
@variable(model, hours, Int)
@variable(model, days, Int)
@constraint(model, days / 24 >= hours - 1)

```

If your objective minimizes `days * cost` then the solver will push `days` down to the rounded value. I’m not entirely sure about the rounding direction but fiddling with a `+1` or `-1` should work eventually

---

<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:** [December 26, 2023, 8:50pm UTC](https://discourse.julialang.org/t/round-off-decision-variable/107988/3 "2023-12-26T20:50:57Z")

</div>

Assuming `start_n` is the number of hours, do:

```julia
using JuMP
n = 10
model = Model()
@variable(model, start_n[1:n] >= 0, Int)
@variable(model, 0 <= hours[1:n] <= 23, Int)
@variable(model, 0 <= days[1:n], Int)
@constraint(model, [i in 1:n], start_n[i] == 24 * days[i] + hours[i])

```

Edit: opened a pr to improve the docs: [[docs] add modulo to tips\_and\_tricks.jl by odow · Pull Request #3641 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/pull/3641)
