Using fractions and manipulating them

Hi all, is there any way to work with fractions? I am wanting to add inches (5 3/4) to cells and add them together, round down to the nearest 1/8th.

This is exactly why I love the metric system!!! :smile:

Hello Sergio, welcome to the community!

Great question. Grist doesn’t provide a built-in way to work with fractions, but luckily, Python has a module that makes it quite possible. I found an example we had of converting decimal numbers to fractions here Fractions - Grist, and I added a second page to it for adding inches.

The Python formulas needed to do that are a little wild because there aren’t convenient methods for mixed fractions, or for rounding fractions. You can double-click on a cell to see the formula in it. Pasting here the formula for “Sum rounded down to 1/8” for reference:

from fractions import Fraction

# Parse mixed fractions (like "1 1/2")
def mixed(text):
  return sum(Fraction(part) for part in text.split())

result = mixed($A) + mixed($B)

# Round down to nearest 1/8
result = math.floor(result * 8) / Fraction(8)

# Format the result as a mixed fraction.
whole = math.floor(result)
" ".join(str(p) for p in [whole, result - whole] if p)