Loading video player…

Welcome to the Month-End Invoices

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:

Language: Python
# ... 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:

Language: Program Output
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:

Language: JavaScript
// ... 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:

Language: Program Output
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 Infinity or NaN
  • 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.

Download

Sample Code (.zip)

12.4 KB
Download

Course Slides (.pdf)

772.3 KB

00:00 Welcome to JavaScript Number Precision, BigInt, and Formatting for Pythonistas! It’s about where JavaScript’s number type stops matching what Python’s float, int and Decimal do.

00:12 Python’s numbers do careful work; a port can run, look right, and still be wrong. You’ll port a coffee roastery’s month-end invoice script, which reads its web shop’s export and checks the shop’s total.

00:25 Here’s a first taste. Python checks the month’s total against the shop’s, and prints a match. In the line-by-line port, two Decimal() calls become Number() calls.

00:38 JavaScript prints the same amounts, then asks you to check the export. Both totals read €1,303.20. There’s no error: this course is about differences like that.

00:53 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. The order dashboard runs on JavaScript, so the script is moving there too.

01:09 You’ll add up weights and money exactly, keep long order numbers intact, catch Infinity and NaN results, and format numbers so they round the way Python’s do.

01:18 Then you’ll port the whole script.

01:23 You need to know Python’s Decimal and format specs, and JavaScript’s Math, Number() and Intl.NumberFormat, for currency and percents.

01:32 Any current Node is fine.

01:36 Videos mix with text lessons and exercises, and every video has a summary below it. A quiz and a summary close the course. Next, in the text lesson Adding Up Weights Without sum(), you’ll port the month’s total weight.

Become a Member to join the conversation.