# Round float addition

**URL:** <https://discourse.julialang.org/t/round-float-addition/73130>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 15, 2021, 10:16am UTC](https://discourse.julialang.org/t/round-float-addition/73130 "2021-12-15T10:16:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![n90](https://avatars.discourse-cdn.com/v4/letter/n/439d5e/32.png) [@n90](https://discourse.julialang.org/u/n90)\
**Post date:** [December 15, 2021, 10:16am UTC](https://discourse.julialang.org/t/round-float-addition/73130/1 "2021-12-15T10:16:26Z")

</div>

Can I round 0.1 + 0.2 to 0.30 not the default 0.30000000000000004 in Julia.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [December 15, 2021, 10:19am UTC](https://discourse.julialang.org/t/round-float-addition/73130/2 "2021-12-15T10:19:49Z")

</div>

It sounds like what you actually want is a number type that can accurately represent fractions. How about a Rational number?

```julia
julia> 1//10+2//10
3//10

julia> float(1//10+2//10)
0.3

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [December 15, 2021, 10:56am UTC](https://discourse.julialang.org/t/round-float-addition/73130/3 "2021-12-15T10:56:37Z")

</div>

If you know you have exactly fractions I would go with above suggestion.

If you don’t know that and just want to represent numbers with some amount of significant digits you can do

```julia
julia> round(0.1+0.2, sigdigits=1)
0.3

```

---

<div class="post-metadata">

**Author:** ![n90](https://avatars.discourse-cdn.com/v4/letter/n/439d5e/32.png) [@n90](https://discourse.julialang.org/u/n90)\
**Post date:** [December 17, 2021, 9:01am UTC](https://discourse.julialang.org/t/round-float-addition/73130/4 "2021-12-17T09:01:40Z")

</div>

Hey thanks for the reply. But I am not looking for rational numbers as a solution. Is it possible to round the float result to a lesser precision with dynamic decimal digits. Essentially by not using round() digit or sigdigits option,

---

<div class="post-metadata">

**Author:** ![n90](https://avatars.discourse-cdn.com/v4/letter/n/439d5e/32.png) [@n90](https://discourse.julialang.org/u/n90)\
**Post date:** [December 17, 2021, 9:02am UTC](https://discourse.julialang.org/t/round-float-addition/73130/5 "2021-12-17T09:02:55Z")

</div>

Hey thanks for the reply. I already tried this method. But in various test cases sigdigits can vary. Can I dynamically assign sigdigits some how?

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [December 17, 2021, 9:15am UTC](https://discourse.julialang.org/t/round-float-addition/73130/6 "2021-12-17T09:15:00Z")

</div>

Can you explain what behaviour you want?

Maybe you need to use a decimal floating point type such as [https://github.com/JuliaMath/DecFP.jl](https://github.com/JuliaMath/DecFP.jl).

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [December 17, 2021, 9:31am UTC](https://discourse.julialang.org/t/round-float-addition/73130/7 "2021-12-17T09:31:16Z")

</div>

You have to define the behavior you want. “Dynamic digits”—based on what?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 17, 2021, 9:46am UTC](https://discourse.julialang.org/t/round-float-addition/73130/8 "2021-12-17T09:46:29Z")

</div>

Just in case, for MATLAB-like formatting see a solution in [this post](https://discourse.julialang.org/t/how-to-print-a-number-to-x-digits/50526/3).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 17, 2021, 9:50am UTC](https://discourse.julialang.org/t/round-float-addition/73130/9 "2021-12-17T09:50:35Z")

</div>

In case you are not aware of what is causing this phenomenon, look here: [https://0.30000000000000004.com/](https://0.30000000000000004.com/)  
and here: [PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678)

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [December 17, 2021, 10:55am UTC](https://discourse.julialang.org/t/round-float-addition/73130/10 "2021-12-17T10:55:50Z")

</div>

> [@n90](#):
>
> Hey thanks for the reply. I already tried this method. But in various test cases sigdigits can vary. Can I dynamically assign sigdigits some how?

The problem is that 0.1 with floating points arithmetic is not equal to the real number 0.1, it is only equal if you round it to somewhere between one and n significant digits where n is based on where the difference show up. And since there is a range of valid significant digits to make them equal it is hard to automatically figure out how many you intended to provide, and thus hard to do the calculations with floats and round after in some automated manner.

It sounds like the decimal floating point package mentioned above could be what you are after?
