BETA测试运营中 — 部分功能和商品尚未开放。
实战DEV

定价只发生在一处 — 标签和账本为什么在收档时对不上

按重量卖的店,总有一天会在收档时差出几百块。因为价格被算了两次:一次为了让客人能付而取整,印在标签上;一次从原始重量重算,进了账本。两个都说得通,而且互相不等。

makemind2026年8月4日

按重量卖的店 总有一天会对不上账。

一摞标签加起来,和收银总额差出几百块,找不到是哪儿出的。而且每天都出一点。

为什么会出

因为价格被算了 两次。

  • 打标签的时候 —— 取整到客人能付的单位。算出来 $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」—— 客人并没有付那个数。

秤盘上

秤头 —— Pork belly $2.98 / 100 g,秤盘上 386 g,LABEL $11.50;下面照实写出 exact $11.503 与四舍五入的 −0.28 cent
秤头 —— Pork belly $2.98 / 100 g,秤盘上 386 g,LABEL $11.50;下面照实写出 exact $11.503 与四舍五入的 −0.28 cent

标签上 写着这次取整取掉了多少。

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)

有进位的,有舍去的,也有正好的。 这是故意挑的 —— 用全都朝同一个方向取整的数据去试,这类缺陷根本不会露头。

同一天的账目 —— 2 张标签,合计 $20.30。与秤头同样的格式、同样的数字
同一天的账目 —— 2 张标签,合计 $20.30。与秤头同样的格式、同样的数字
ledger says 2 labels, total $20.30
harness added the labels itself: $20.30
ledger total minus watched labels: $0.00

此内容需要开发者或更高等级

登录并升级您的方案即可继续阅读。

查看方案