A recipe cost calculator sounds trivial: price times quantity. Then you meet the real input. The beans come in a 15.5 oz can and the recipe wants 31 oz. The chili powder is sold by weight and used by the tablespoon. The onions are sold "each". Here's how my free recipe cost calculator handles units, in about ten lines.
One table, three kinds of unit
Every unit converts to a base unit of its own kind: grams for weight, millilitres for volume, and 1 for things you count.
const U = {
g: ['w', 1], kg: ['w', 1000], oz: ['w', 28.3495], lb: ['w', 453.592],
ml: ['v', 1], l: ['v', 1000], tsp: ['v', 4.929], tbsp: ['v', 14.787],
cup: ['v', 236.6], 'fl oz': ['v', 29.574],
each: ['e', 1],
};
Cost of one ingredient
function cost(price, size, sizeUnit, used, usedUnit) {
const [kindA, a] = U[sizeUnit], [kindB, b] = U[usedUnit];
if (kindA !== kindB) return null; // weight vs volume: can't convert
return price * (used * b) / (size * a);
}
Example: a $1.09 can of 15.5 oz beans, and the recipe uses 31 oz. That's 1.09 x 31 / 15.5 = $2.18.
Why weight-to-volume returns null
A cup of flour weighs about 125 g. A cup of sugar weighs about 200 g. A cup of water weighs 237 g. Any single conversion factor would be wrong for most foods. So instead of guessing, the tool says which side is a weight and which is a volume, and asks the user to pick matching units. Use the package's serving info, or weigh the ingredient once. An honest "I can't" beats a confident wrong price.
The two numbers people want
cost per serving = sum of ingredient costs / servings
yearly saving = (takeout price per serving - cost per serving) x servings x 52
For a 6-serving pot of chili at $14.56 ($2.43 a serving) against $14 takeout, making it once a week saves about $3,600 a year. The tool also shows which ingredient takes the biggest share of the cost. Here it's the ground beef, at 45%. That's the first place to look for savings.
To plan a whole week of meals, my Meal Planner spreadsheet (Excel and Google Sheets) works out cost per serving for every recipe and builds the grocery list from the week plan.
Top comments (0)