# Fuzzy rounding for Rationals

**URL:** <https://discourse.julialang.org/t/fuzzy-rounding-for-rationals/67420>\
**Category:** General Usage\
**Tags:** numbers\
**Created:** [August 31, 2021, 11:22am UTC](https://discourse.julialang.org/t/fuzzy-rounding-for-rationals/67420 "2021-08-31T11:22:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [August 31, 2021, 11:22am UTC](https://discourse.julialang.org/t/fuzzy-rounding-for-rationals/67420/1 "2021-08-31T11:22:58Z")

</div>

Is there an easy way to convert `0.005` to a Rational 1//200 ?  
I guess `0.005` has no exact representation in base 2, which is why it is interpreted as this terribly long fraction.

EDIT: **Actually my use case is more “general” than the above example.**  
I have a float which is the result of an Integer division where both arguments have ‘few’ digits (probably less than 6 in general). A better example is `Rational(43/550)`. I.e. given `0.07818181818181819` I would like to obtain 43 and 550.

```julia
julia> a = Rational(1,200); b = Rational(0.005); iszero(float(a)-float(b))
true

julia> a
1//200

julia> b
5764607523034235//1152921504606846976

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [August 31, 2021, 11:31am UTC](https://discourse.julialang.org/t/fuzzy-rounding-for-rationals/67420/2 "2021-08-31T11:31:19Z")

</div>

It is clear to me that `trunc(Int, 1/0.005) == 200`. Please consider the other example

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 31, 2021, 11:50am UTC](https://discourse.julialang.org/t/fuzzy-rounding-for-rationals/67420/3 "2021-08-31T11:50:01Z")

</div>

See the [`rationalize` function](https://docs.julialang.org/en/v1/base/math/#Base.rationalize):

```julia
julia> rationalize(0.005)
1//200

julia> rationalize(43/550)
43//550

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [August 31, 2021, 11:57am UTC](https://discourse.julialang.org/t/fuzzy-rounding-for-rationals/67420/4 "2021-08-31T11:57:45Z")

</div>

Thanks!
