Welcome to the Month-End Invoices
This course is about where JavaScript’s number type stops matching what Python’s float, int
and Decimal do. Python’s numbers do careful work; a port can run, look right, and still be wrong. Along
the way, you’ll port a coffee roastery’s month-end invoice script.
Here’s a first taste. These are the script’s money lines in Python, with the setup and the two messages folded into comments:
# ... CAFES, orders, CENT = Decimal("0.01")
month_total = Decimal("0")
for cafe in CAFES:
price = Decimal(cafe["price_per_kg"])
amount = Decimal("0")
for order in orders:
if order["cafe"] == cafe["name"]:
amount += price * Decimal(str(order["kg"]))
amount = amount.quantize(CENT, rounding=ROUND_HALF_UP)
month_total += amount
print(f"{cafe['name']}: €{amount:,.2f}")
if month_total == Decimal(shop_export["total"]):
... # print "Month total ... matches the web shop"
else:
... # print "Check the export: ... here, ... in the shop"
Python checks the month’s total against the shop’s, and prints a match:
Larder Lane: €666.00
Bluebell Café: €172.80
Northside Kitchen: €464.40
Month total €1,303.20 matches the web shop
In the line-by-line port, two Decimal() calls become Number() calls, on the prices and the shop’s
total:
// ... CAFES, orders, euros: the euro formatter
let monthTotal = 0;
for (const cafe of CAFES) {
const price = Number(cafe.pricePerKg);
let amount = 0;
for (const order of orders) {
if (order.cafe === cafe.name) {
amount += price * order.kg;
}
}
amount = Math.round(amount * 100) / 100;
monthTotal += amount;
console.log(`${cafe.name}: ${euros.format(amount)}`);
}
if (monthTotal === Number(shopExport.total)) {
// ... log "Month total ... matches the web shop"
} else {
// ... log "Check the export: ... here, ... in the shop"
}
JavaScript prints the same amounts, then asks you to check the export:
Larder Lane: €666.00
Bluebell Café: €172.80
Northside Kitchen: €464.40
Check the export: €1,303.20 here, €1303.20 in the shop
Both totals read €1,303.20. There’s no error: this course is about differences like that.
The roastery sells most of its coffee to cafés. Each month, the roastery’s bookkeeper runs the script on the web shop’s export. It totals the month’s weight and prints each café’s invoice line: kilos, share, amount, a payment reference, and the café’s orders. The order dashboard runs on JavaScript, so the script is moving there too.
By the end of this course, you’ll be able to:
- Add up weights and money so the totals match Python’s exactly
- Keep 19-digit order numbers exact, and compute check digits with BigInt
- Catch a result that comes out as
InfinityorNaN - Format shares, signs and units, rounding the way Python does
- Port the whole invoice script to JavaScript
You need to know Python’s Decimal and format specs such as :.2f and :.0%, and that floats don’t
always add up exactly and ints have no size limit. In JavaScript, you need to know its number
type, Math, Number(), and Intl.NumberFormat with the currency and percent styles. You need
Python 3.12 or later and any current version of Node: the examples use Node 26 and Python 3.15.
The course mixes videos with text lessons and exercises, and every video has a summary below it. A quiz and a summary close the course.
Up next is the text lesson Adding Up Weights Without sum(), where you’ll port the month’s total weight.