按重量卖的店 总有一天会对不上账。
一摞标签加起来,和收银总额差出几百块,找不到是哪儿出的。而且每天都出一点。
为什么会出
因为价格被算了 两次。
- 打标签的时候 —— 取整到客人能付的单位。算出来 $11.503 的东西打成 $11.50。
- 进账本的时候 —— 从重量重新算一遍。$11.503。
两个都没错。可它们不一样。而且每一单的差额不足 1 分,要过一天才看得出来。
把定价的地方收成一处
这个样例的答案是一个函数。
/// The only place a price is made.
///
/// Rounds to what somebody can pay, once. Every other number in this server
/// — the label, the ledger, the day total — comes from this and never from
/// a second pass over the weight.
static int priceFor(Product p, int grams) {
final exact = p.centsPer100g * grams / 100;
return (exact / _payableStep).round() * _payableStep;
}
而且打标签的时候,这一次调用的结果原样进账本。
// One call, one number. The label and the ledger row are the same
// integer, so there is nothing for them to disagree about.
final cents = priceFor(_product, _grams);
_sales.add(Sale(_product.code, _product.name, _grams, cents));
客人付的金额,就是账本上留下的金额。 账本没有理由说「严格来讲是 $11.503」—— 客人并没有付那个数。
秤盘上

标签上 写着这次取整取掉了多少。
pan: Pork belly 386 g — exact $11.503, label $11.50 (rounded -0.28 cent to the nearest cent)
// What the rounding actually did, in cents, said out loud. A shop
// that rounds without showing it is a shop that argues later.
这不是给客人看的,是 给店看的。 把取整藏起来,将来有人问「怎么算成这样」,没人答得上。摆出来,问题当场就完了。
五单,然后收档
label 1: Pork belly 386 g -> $11.50 (exact 11.503, rounded -0.28 cent)
label 2: Beef brisket 212 g -> $8.80 (exact 8.798, rounded +0.20 cent)
有进位的,有舍去的,也有正好的。 这是故意挑的 —— 用全都朝同一个方向取整的数据去试,这类缺陷根本不会露头。

ledger says 2 labels, total $20.30
harness added the labels itself: $20.30
ledger total minus watched labels: $0.00