I'm writing a recipe app that will need to perform addition/subtraction on ingredients to make a master shopping list. What I can't figure out is how to represent a fraction like 1/3 (which doesn't convert to a clean decimal) in a way that won't result in .9999999.... instead of 1. I can't even use ounces as a base unit because a third of a cup is 2.6666666 oz.
In short, the idea is that if the user is making recipes that use sugar, they may have one calling for a cup, another for half a cup, and another with a third of a cup. This would then tell them they need to get 1 5/6 cups of sugar on their next shopping trip.
Has anyone got a fix for this, or should I just ban all recipes with ugly fractions (which is far from ideal)?
The closest thing I've come up with so far is to store every ingredient as multiples of a third of an ounce, but that makes the backend logic a bit complicated!
Bonus points if the fix will allow me to easily store the value in a Postgres database!