A checkout rule promises a retailer at least 30% gross margin. The item costs $72. At a selling price of $102.85 the UI shows 30.00%. At $102.86 it also shows 30.00%. Only the second price meets the promise.

The first fraction is 30.85 / 102.85, or about 29.9951%. The second is 30.86 / 102.86, or about 30.0019%. Using decimal arithmetic fixes binary representation errors. It still lets the cheaper price through if the checkout compares the formatted percentage to 30.

Turn the promise into an integer comparison

Store prices in cents and the target in basis points. A 30% target is 3,000 basis points. For a positive selling price, you can multiply both sides of the margin inequality without changing its direction.

(price - cost) × 10,000 >= price × target price × (10,000 - target) >= cost × 10,000 

The second line gives the minimum price. Divide the right side by the factor on the left and round up to a whole cent. For a positive denominator, adding the denominator minus one before integer division gives that ceiling. An exact whole-cent price stays put.

Test the cent on either side

This complete Python example takes integer inputs, a positive unit cost, and a target from 0 through 9,999 basis points. A 100% margin has no finite selling price when cost is positive. The example uses one item's pretax sale price after any coupon.

from decimal import Decimal, ROUND_HALF_UP from fractions import Fraction BPS = 10_000 def minimum_price(cost, target): if cost <= 0 or not 0 <= target < BPS: raise ValueError("expected positive cost and a target in [0, 10000)") denominator = BPS - target return (cost * BPS + denominator - 1) // denominator def meets(price, cost, target): return price > 0 and (price - cost) * BPS >= price * target def display_margin(price, cost): pct = Decimal(100) * (price - cost) / price return pct.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) cost, target = 7200, 3000 assert minimum_price(cost, target) == 10286 assert not meets(10200, cost, target) for price in (10285, 10286): shown = display_margin(price, cost) print(f"{price} cents: {shown}%, passes={meets(price, cost, target)}") cases = 0 for cost in range(1, 2501): for target in (0, 1, 500, 1250, 3000, 3333, 5000, 7500, 9900, 9999): price = minimum_price(cost, target) exact = Fraction(cost, 1) / (1 - Fraction(target, BPS)) assert price - 1 < exact <= price assert meets(price, cost, target) assert not meets(price - 1, cost, target) cases += 1 print(f"{cases:,} minimum-price cases passed") 

Output with Python 3.14.4 and its standard library

10285 cents: 30.00%, passes=False 10286 cents: 30.00%, passes=True 25,000 minimum-price cases passed 

There are two separate checks in the loop. An exact rational number supplies a reference price independent of the integer ceiling formula. Then the checkout predicate must accept the chosen cent and reject the cent immediately below it. A test that only checks acceptance would also pass a function that overcharges every shopper by a cent.

For the jacket in this example, a 15% coupon on a $120 tag produces $102.00. That's the price to send to the guard after the till applies its discount and cent-rounding rules. Formatting belongs after that decision.

An AI agent drafted this article and ran the complete example. All amounts are synthetic.

For a second implementation to check by hand, enter a cost of $72 and a 30% target in Bokeping’s selling-price calculator.