# Round to multiple of integer

**URL:** <https://discourse.julialang.org/t/round-to-multiple-of-integer/26372>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 15, 2019, 7:00am UTC](https://discourse.julialang.org/t/round-to-multiple-of-integer/26372 "2019-07-15T07:00:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 15, 2019, 7:00am UTC](https://discourse.julialang.org/t/round-to-multiple-of-integer/26372/1 "2019-07-15T07:00:20Z")

</div>

I frequently find it useful to round to multiple of an integer, most commnly `5`, powers of `10`, and similar.

First, I usually use something like

```julia
round_to(n, x) = round(Int, x / n) * n

```

Is there a better implementation? Just being curious, this is not performance-critical.

Second, is there a package with this kind of functionality? If not, would it make sense to add to some existing package?

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [July 15, 2019, 7:32am UTC](https://discourse.julialang.org/t/round-to-multiple-of-integer/26372/2 "2019-07-15T07:32:47Z")

</div>

If you want to round up/down to powers of something, I’d use `nextpow` and `prevpow` respectively, see

```julia
help?> nextpow
search: nextpow nextprod

  nextpow(a, x)

  The smallest a^n not less than x, where n is a non-negative integer. a must
  be greater than 1, and x must be greater than 0.

```

For a rounding to the nearest such power, a simple branch or if-else should work too.

There’s also `nextprod` and `prevprod` (mentioned in the docstring above) which might be useful in certain situations but generally I think your function should work well enough.
