Convert Fractions stored as text to decimal

I looked for a function or some other way to do this, but if I have fractions stored as text, how can I convert them to decimal values.

Say I have:
1/2
3/4
7/8
78/192
2 3/8
4-1/4

The eval() function can help in this case. See the example below:

evaluate-fractions.morph (4.2 KB)
image

Iterations are required, because the argument of the eval() function must be constant for each row, so it can't use field references.

1 Like

might need to first remove any dashes so they are not interpreted as minus.

If a dash means addition of the integer and fractional parts, then should be converted to "+" for correct evaluation. In my example above I only converted spaces to pluses.

And your replace([Fractions], " ", "+") step already handles the case where there is a space between the whole number and the fraction. Good stuff.

Followup to this - now is there a way to convert the decimal to a fraction?

I would do it as follows:

  • Create a lookup table for all possible fractional parts, for instance:

    Decimal Fraction
    0.75 3/4
    0.5 1/2
  • Separate the fractional part from each number

  • Use lookup to obtain fractions from decimal fractions

  • Prepend with the integer part:

    truncate([Number]) & " " & [Fraction]