Unit Price Calculator
Compare prices per unit to find the best deal between different package sizes.
About this tool
The Unit Price Calculator helps you find the best deal by comparing the price per unit across packages of different sizes. Stores often sell the same product in different quantities, so the item with the lower sticker price is not always the better value. This tool cuts through the confusion by revealing the true cost per unit.
Enter the price and the size or quantity for two or more options, and the calculator shows the price per unit for each along with which one is cheapest. It is handy for grocery shopping, buying in bulk, and comparing different brands or pack sizes side by side.
A useful tip is that comparing the per-unit price often reveals when a bigger pack is actually the worse deal, despite seeming economical. Everything runs locally in your browser, so your numbers never leave your device.
Frequently Asked Questions
Code Implementation
# Unit Price Calculator β find the best value
def unit_price(price, quantity, unit="g"):
"""Calculate price per unit."""
return price / quantity
def compare_products(products):
"""
products: list of dicts with 'name', 'price', 'quantity', 'unit'
Returns products sorted by unit price (best value first).
"""
results = []
for p in products:
up = unit_price(p["price"], p["quantity"])
results.append({**p, "unit_price": up})
return sorted(results, key=lambda x: x["unit_price"])
# Example: comparing 3 coffee products
products = [
{"name": "Brand A 200g", "price": 3.99, "quantity": 200, "unit": "g"},
{"name": "Brand B 500g", "price": 8.49, "quantity": 500, "unit": "g"},
{"name": "Brand C 1kg", "price": 15.99, "quantity": 1000, "unit": "g"},
]
ranked = compare_products(products)
for i, p in enumerate(ranked, 1):
print(f"{i}. {p['name']}: ${p['unit_price']*100:.4f} per 100{p['unit']}")
# Output:
# 1. Brand C 1kg: $1.5990 per 100g (best value)
# 2. Brand A 200g: $1.9950 per 100g
# 3. Brand B 500g: $1.6980 per 100gComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.