Whole-Cent Integer Math for Fee and Repricing Engines
See why fee and repricing engines should count in integer cents and basis points, with real floating-point failures and the rounding rules that fix them.
A fee calculator or a repricer does one thing over and over: it multiplies money by a percentage and rounds the result. Do that with ordinary floating-point numbers and, sooner or later, it produces a price that is a cent wrong. A cent sounds harmless. Across thousands of listings repriced every few hours, it becomes a pattern of overcharging buyers or undercutting your own margin, and it makes the numbers impossible to reconcile with the marketplace's invoice. This is how we avoid it in the eBay Fees Calculator and in DS Repricer, which is in development.
Where floating point goes wrong
JavaScript, like most languages, stores numbers as binary fractions. Most decimal amounts have no exact binary form, so they are stored as the nearest value that does. You can see it directly:
0.1 + 0.2is0.30000000000000004.19.99 * 100is1998.9999999999998, not1999.Math.round(1.005 * 100) / 100is1, not1.01, because1.005is stored as slightly less than itself.
Each of those errors is tiny. The trouble starts when the code then rounds in a fixed direction, because rounding magnifies a tiny error into a whole cent.
A repricing example that charges a cent too much
A repricer finds the lowest price that keeps a target profit after the marketplace fee:
price = (cost + profit + fixed fee) / (1 - fee rate), rounded up to the cent
Take a cost of 2.89, a target profit of 5.00, a fixed fee of 0.30 and a 10% fee. The exact answer is 8.19 / 0.9 = 9.10, already a whole cent. In floating point, 2.89 + 5 + 0.30 is 8.190000000000001, the division gives 9.100000000000001, and rounding up to the cent produces 9.11. The code has asked the buyer for a cent more than the rule says, for no reason at all. At a 10% fee, a search over ordinary costs finds the same one-cent error at 2.98, 3.07, 3.43, 4.15 and many more.
Count in cents, rate in basis points
The fix is to never hold money as a fraction at all:
- Money is an integer number of cents (or the smallest unit of the currency). 9.10 is
910. - Rates are integer basis points, where 1% is 100 and 13.6% is 1,360.
- A percentage of an amount is
amount x bps / 10,000, computed in integers, with the rounding chosen explicitly.
The same price, in integers: ceil((289 + 500 + 30) x 10,000 / (10,000 - 1,000)) = ceil(8,190,000 / 9,000) = 910. No representation error is possible, because nothing in the calculation is a fraction until the final, deliberate division.
Converting user input needs the same care. Math.round(parseFloat("19.99") * 100) gives 1999, which is right, but only because of the Math.round; truncating with Math.floor would give 1998. Parse the string, round once, and from then on stay in integers.
Choose the rounding direction on purpose
Integers do not remove rounding; they make it visible, so you can decide which way each step goes.
- Round fees and costs up. If a fee is 12.9% of 10.05, the exact fee is 1.29645. A pricing engine that rounds it up to 1.30 can only ever leave the seller with a fraction of a cent more profit than planned, never less. Rounding to nearest would sometimes leave less.
- Search for the price, then prove it. Rounding the fee up inside the price formula means the closed-form answer is occasionally a cent off. The robust approach is to compute the closed form, then check the neighbouring cents: the price must keep the target profit, and one cent less must not.
- Round display figures to nearest. A margin percentage shown to a person is not money that moves, so half-up rounding is fine there.
- Guard against overflow.
amount x bpscan exceed the range where doubles are exact (about 9 x 10^15) for large amounts. ABigIntfor that one multiplication costs nothing and removes the limit.
Round once, at the level the marketplace rounds
There is a second, quieter source of drift: rounding the same fee at different levels. Three items at 1.00 each, with a 13.6% fee:
- Rounded per item: 13.6 cents becomes 14 cents, three times, which is 42 cents.
- Rounded on the order total of 3.00: 40.8 cents becomes 41 cents.
Neither is wrong in isolation; they are two different rules. If the marketplace charges its percentage on the order total and your engine rounds per line, your profit report and the invoice will disagree by a cent on some orders, and nobody will be able to say why. Find out where the marketplace rounds, and round there, once.
Test the invariants, not just examples
Example-based tests check the cases you thought of. For money code, also test the properties that must hold for every input:
- The computed price always keeps at least the target profit.
- One cent less than the computed price never does.
- A price ending such as .99 only ever moves the price up.
- Changing the order of additions never changes the result.
A brute-force check over a few thousand costs, fee rates and fixed fees runs in milliseconds and catches exactly the class of bug described above. The DS Repricer pricing engine is tested against these invariants, and the calculator's tests assert each marketplace's own worked examples where eBay publishes them. If you want to see the fee side of a price, the eBay Fees Calculator shows every fee line in whole cents, and its final value fee comparison runs the same sale through all 19 marketplaces.
