The formula
Unit price = price ÷ quantity
The item with the lower unit price is better value, whatever the shelf price says.
Worked example
Item A costs 2.40 for 500 g, which is 0.0048 per gram. Item B costs 4.20 for 1,000 g, which is 0.0042 per gram. Item B is 12.5% cheaper per gram.
Use the same unit
Compare grams with grams or millilitres with millilitres. If one pack is in kilograms and the other in grams, convert first: 1 kg is 1,000 g.
Cheaper per unit is not always the better buy
A larger pack only saves money if you use it all. For food that spoils, the smaller pack can cost less in practice.