Method Deck / currency

Unit price comparison

Computes the price per unit for two package sizes and reports which package is the better value.

Published for review. The Founder is checking every card himself; anything he strikes comes down the same hour.

Try it

Inputs

Outputs

Steps

  1. Compute the unit price of package A: unit_price_a = price_a / quantity_a.
  2. Compute the unit price of package B: unit_price_b = price_b / quantity_b.
  3. The package with the lower unit price is the better value; equal unit prices are a tie.
  4. Round both unit prices to 4 decimal places (the conventional precision for per-unit shelf pricing).

Constants

None.

Sources

Worked example and test vectors

NameInputsAnswer
worked example{"price_a":4.99,"quantity_a":16,"price_b":7.99,"quantity_b":32}Package A: 0.3119 per unit; Package B: 0.2497 per unit; package B is cheaper per unit.
test vector: A is cheaper{"price_a":3,"quantity_a":12,"price_b":5,"quantity_b":18}Package A: 0.2500 per unit; Package B: 0.2778 per unit; package A is cheaper per unit.
test vector: tie{"price_a":10,"quantity_a":5,"price_b":20,"quantity_b":10}Package A: 2.0000 per unit; Package B: 2.0000 per unit; the two packages are the same price per unit.

Published card