---------------------------------------
Formatting fractions in columns of text
---------------------------------------

by Larry Hastings
2023/05/31

A quick treatise on how I present fractions to the user
in a monospaced font (e.g. in stdout).  starvote presents
these for scores, and for averages.  Note that both of
these are always presented in columns, in sorted order
with larger number above smaller numbers.

First, I decided I wanted to "normalize" the fractions,
so that the fractional part was always less than one.
I'd then present them as a short expression, in four
columns:

       +++----------- integer
       ||| +--------- plus sign
       ||| | +++----- numerator
       ||| | |||++++- denominator
       ||| | |||||||
       vvv v vvvvvvv
       123 + 456/789

(I merged the divisor slash into the denominator column.)

After playing with that a little, I decided that smooshing
all the text together was better.  When you don't have an
integer component, the spaces just put the fraction way too
far right.  Without spaces seemed like it was never less
readable, and was often more readable, because your eye
didn't have to travel as far.

       123 + 456/789
              11/789

vs

       123+456/789
            11/789

I then discovered a funny boundary case: what if every
number had either an integer or a fraction, but never
both?  The way I'd done my formatting, this meant the
"plus sign" column was zero width.  The result looked funny:

       123
         1
          11/789
          45/77

Final approach
--------------

What I settled on is two different formats...
maybe three.

If there's *never* a plus sign in the output, then the
integer and the numerator get put in the same column,
aligned right:

       123
         1
        11/789
        45/77

And if there are *any* plus signs in the output, then
there are two phases of the output.  First, the lines
where we display an integer (the integer is 1 or more):

       123+456/789
        11+ 22/33
         2
         1+  3/5

The width of each of these columns is independently
measured; integer and numerator are aligned right,
denominator is aligned left.  The plus signs and
slashes, when present, line up.

After that we display the fraction-only lines.  These
are independently measured and aligned on their own.

      1143/2025
        45/77

Note that you don't want to use the same width for the
"with an integer" denominator and the "without an
integer" denominator.  This can look funny:

      22+  33/45
       1+   2/5
      1143/2025

This reads nicely and makes sense visually.  There are
the lines with integers, and then the lines without
integers, and the change in format helps draw your
attention to the discontinuity.
