wolph / alfred-converter

Alfred unit converter is a smart calculator for Alfred with support for unit conversions to make it a bit comparable to the Google Calculator and Wolfram Alpha.
93 stars 15 forks source link

Filter the denominators that can be shown for fractional units #45

Closed aaronkollasch closed 1 year ago

aaronkollasch commented 1 year ago

This PR filters the denominators that can be shown for fractional units, showing decimals otherwise. While units like inches are commonly written as fractions, e.g., "5/8 inch", large denominators are uncommon.

For example, "57 mm to in" currently produces the response "57 millimeters = 285/127 inch", which I don't find very helpful. This PR changes the output to "57 millimeters = 2.244094 inch".

It also changes split units, for example "1357 mm to ft" outputs "1357 millimeters = 4.4521 foot" instead of:

1357 millimeters = 6785/1524 foot 1357 millimeters = 4 689/1524 foot 1357 millimeters = 4 foot 689/127 inch 1357 millimeters = 4 foot 5 54/127 inch

Split units with allowed denominators still work, for example "113.125 in to ft" gives:

113.125 inch = 9.427083 foot 113.125 inch = 9 foot 41/8 inch 113.125 inch = 9 foot 5 1/8 inch

instead of:

113.125 inch = 905/96 foot 113.125 inch = 9 41/96 foot 113.125 inch = 9 foot 41/8 inch 113.125 inch = 9 foot 5 1/8 inch

And "113 in to ft" gives:

113 inch = 9.416667 foot 113 inch = 9 foot 5 inch

instead of:

113/12 foot 113 inch = 9 5/12 foot 113 inch = 9 foot 5 inch

Note that the denominator 12 is excluded as the inches split unit is more useful anyways.

An alternate solution would be to append the decimal as an additional output, but I think that would clutter the results, and denominators like 96 or 127 probably aren't useful to many people anyway.

The ALLOWED_DENOMINATORS set is just an initial thought, so I'm open to additions or removals.

wolph commented 1 year ago

Silly that I didn't think of this. Very nice addition, thank you!