# Rounding to a multiple of float

**URL:** <https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295>\
**Category:** Numerics\
**Created:** [February 28, 2019, 11:25am UTC](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295 "2019-02-28T11:25:27Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 28, 2019, 6:12pm UTC](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295/21 "2019-02-28T18:12:02Z")

</div>

You’re right… OP: It is generally discouraged to use internal functions, since they might disappear or change behavior in future versions of Julia, and they’re also undocumented which makes your code harder to read. If the problem can be solved by using the publicly exported API that would be preferred.

In this case, there is indeed no reason to use `_round_invstep`, since that method simply boils down to:

```julia
_round_invstep(x, invstep, r::RoundingMode) = round(x * invstep, r) / invstep

```

This is almost identical to your initial suggestion, but with the step inverted. So you could modify your `roundmult` like this:

```julia
julia> roundmult(val, prec) = (inv_prec = 1 / prec; round(val * inv_prec) / inv_prec);

julia> roundmult(3811.47123123, 0.01)
3811.47

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [February 28, 2019, 6:42pm UTC](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295/22 "2019-02-28T18:42:57Z")

</div>

> [@Tamas\_Papp](#):
>
> Designing an interface that accepts `0.3` but not `0.30000000000000004` but otherwise works with `Float64` or similar internally is just absurd.

Unfortunately, it appears like OP needs to deal with exactly such an absurd third-party interface.

So the real question is not “round to multiple of float” but instead “how to implement a rounding mode that successfully interfaces with this absurd third-party interface”.

I would not be entirely surprised if this third-party interface was emergent instead of intended: Maybe there are multiple rounds of “printf” and parsing with different kind of rounding modes / format-strings involved, maybe crossing `Float32` or decimal or integer representations on some intermediate database servers that all subtly misbehave and combine to create such a nightmare scenario.

In order to interface with such an endpoint, one first needs to understand what it is actually doing, and then work around its bugs. If lucky by finding a source-line that is responsible for the problem, if unlucky by asking support or hammering their webserver. @bennedich implements a clean and principled function; but it is unlikely for a principled and clean solution to exist for this kind of suck.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [February 28, 2019, 7:15pm UTC](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295/23 "2019-02-28T19:15:25Z")

</div>

something like this?

```julia
function roundmult(num,mul)
    if mul == 1
        return round(num)
    elseif 0 < mul < 1
        return round(num/mul)/round(1/mul)
    else
        return round(round(num/mul)*mul)
    end
end 

```

output:

```julia
julia> roundmult(38112.47123123, 1000)
38000.0
julia> roundmult(38112.47123123, 0.01)
38112.47
julia> roundmult(1.06, 0.05)
1.05
julia> roundmult(1.09, 0.05)
1.1

```

the part for numbers greater than one can be improved. for a digits alternative:

```julia
function roundmult(num,digits)
mul = 10^-digits    
    if mul == 1
        return round(num)
    elseif mul < 1 #the exponenciation takes care of negative numbers in mul
        return round(num/mul)/round(1/mul)
    else
        return round(round(num/mul)*mul)
    end
end 

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 28, 2019, 8:48pm UTC](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295/24 "2019-02-28T20:48:42Z")

</div>

> [@cedeerwe](#):
>
> As far as I understand, the problem isn’t that Julia cannot represent `0.3` . The problem is that `0.1 * 3` does not give `0.3` , due to the representation and arithmetic on these two numbers. Am I wrong?

I think this is an accurate description, with the understanding that `0.3` here is, as Tamas points out above, not exactly `0.3`, but the closest `Float64` representation of `0.3`, which is `5404319552844595/2^54 = 0.299999999999999988897769753748434595763683319091796875`. Since Julia knows that this is the closest float to `0.3`, it will display and parse it as “0.3”.

Meanwhile, `0.1` is represented as `3602879701896397/2^55`, so `0.1 * 3` becomes `10808639105689191/2^55`, which rounds to `5404319552844596/2^54`, which is not the same as above, i.e. not the closest number to `0.3`, and thus it will display in full: `0.30000000000000004`.

---

<div class="post-metadata">

**Author:** ![cedeerwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cedeerwe/32/8552_2.png) [@cedeerwe](https://discourse.julialang.org/u/cedeerwe)\
**Post date:** [February 28, 2019, 9:47pm UTC](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295/25 "2019-02-28T21:47:46Z")

</div>

Thanks for all the inputs, I have learned a lot APIs, floats & related stuff.

> [@bennedich](#):
>
> This is almost identical to your initial suggestion, but with the step inverted. So you could modify your `roundmult` like this:
> 
> ```julia
> julia> roundmult(val, prec) = (inv_prec = 1 / prec; round(val * inv_prec) / inv_prec);
> 
> julia> roundmult(3811.47123123, 0.01)
> 3811.47
> 
> ```

As always, floating point arithmetic brings magic. I will rewrite my code to use this instead of the internal function.

I understand that the original question was ill-posed and that the external API is possibly not very well designed, but I appreciate that so many replies have been posted with many important observations. Thanks to everybody!

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [February 28, 2019, 9:52pm UTC](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295/26 "2019-02-28T21:52:22Z")

</div>

You might find this useful: [PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678)

[Previous page](https://discourse.julialang.org/t/rounding-to-a-multiple-of-float/21295.md?page=1)
