The bookkeeper’s month-end script opens with one line about the coffee itself: October’s total weight and how many 12 kg drum loads it took. Porting it looks like a few minutes’ work, since it only adds up eight weights, divides by twelve, and rounds up.
In Python, that line leans on sum(), which does more careful work than its name suggests. In this lesson, you’ll see whether the JavaScript port counts the same drum loads and how to add up measured values exactly.
Start From the Same Doubles
A JavaScript number is the same IEEE 754 double as a Python float, with the same rounding. IEEE 754 is the floating-point standard from the Institute of Electrical and Electronics Engineers. So Node’s REPL (read-eval-print loop) gives the answers you already know:
> 0.1 + 0.2
0.30000000000000004
> (0.1).toFixed(20)
'0.10000000000000000555'
Python’s f"{0.1:.20f}" shows the same twenty digits. A double can’t hold 0.1 exactly, so it stores the nearest double, a hair above. Every weight in the export is slightly off like this.
What differs is the help Python gives you around those doubles. JavaScript has no sum() and no math.isclose(), so the careful part is yours.
Count the Drum Loads
In the bookkeeper’s script, which imported math and set up orders, MONTH, and DRUM_KG earlier, the line totals the kilos with sum() and rounds the drum loads up with math.ceil():
total_kg = sum(order["kg"] for order in orders)
drum_loads = math.ceil(total_kg / DRUM_KG)
print(
f"{MONTH}: {total_kg:.1f} kg in {len(orders)} orders, "
f"{drum_loads} drum loads"
)
For October, it prints October 2026: 72.0 kg in 8 orders, 6 drum loads. Without sum(), a line-by-line port adds the weights with a for...of loop and +=. Arrays have a method that folds an array into one value, but that’s for later. Here’s the port, with the eight orders cut down to the properties this line needs:
const MONTH = "October 2026";
const DRUM_KG = 12;
const orders = [
{ cafe: "Larder Lane", kg: 12.6 },
{ cafe: "Larder Lane", kg: 11.6 },
{ cafe: "Bluebell Café", kg: 4.8 },
{ cafe: "Northside Kitchen", kg: 9.7 },
{ cafe: "Bluebell Café", kg: 4.2 },
{ cafe: "Northside Kitchen", kg: 8.9 },
{ cafe: "Larder Lane", kg: 11.8 },
{ cafe: "Northside Kitchen", kg: 8.4 },
];
let totalKg = 0;
for (const order of orders) {
totalKg += order.kg;
}
const drumLoads = Math.ceil(totalKg / DRUM_KG);
console.log(
`${MONTH}: ${totalKg.toFixed(1)} kg in ${orders.length} ` +
`orders, ${drumLoads} drum loads`,
);
Seventy-two kilos is exactly six drums. Predict the line before you run it.
The port says seven:
October 2026: 72.0 kg in 8 orders, 7 drum loads
Logged on its own, totalKg is 72.00000000000001. Each += rounds to the nearest double, and over eight additions the errors push the total slightly past 72. Then .toFixed(1) rounds it to 72.0 for display, as Python’s :.1f would, so the weight looks right. Math.ceil() sees the real value, a little over six drums, and rounds up to seven.
Python’s own += loop gives 72.00000000000001 too. What differs is sum(). In Python 3.12 and later, sum() over floats compensates for the rounding of each addition, so its total is far more accurate, in this case exactly 72.0.
Add Up Grams Instead of Kilos
The weights come from a scale that reads to 0.1 kg, so each one is a whole number of grams. Whole numbers are exact in a double, and adding them stays exact below 2 ** 53, a limit for a later lesson. So convert each weight to grams where it comes in, add grams, and divide by 1000 only to display kilos.
Round each conversion with Math.round() because multiplying a stored double by 1000 can land a hair off the whole number. For example, 1.005 * 1000 gives 1004.9999999999999:
const MONTH = "October 2026";
const DRUM_KG = 12;
const orders = [
{ cafe: "Larder Lane", kg: 12.6 },
{ cafe: "Larder Lane", kg: 11.6 },
{ cafe: "Bluebell Café", kg: 4.8 },
{ cafe: "Northside Kitchen", kg: 9.7 },
{ cafe: "Bluebell Café", kg: 4.2 },
{ cafe: "Northside Kitchen", kg: 8.9 },
{ cafe: "Larder Lane", kg: 11.8 },
{ cafe: "Northside Kitchen", kg: 8.4 },
];
let totalGrams = 0;
for (const order of orders) {
totalGrams += Math.round(order.kg * 1000);
}
const drumLoads = Math.ceil(totalGrams / (DRUM_KG * 1000));
const totalKg = totalGrams / 1000;
console.log(totalGrams);
console.log(
`${MONTH}: ${totalKg.toFixed(1)} kg in ${orders.length} ` +
`orders, ${drumLoads} drum loads`,
);
The total is a whole 72000 grams, the drum count never leaves grams, and the drum loads match Python’s:
72000
October 2026: 72.0 kg in 8 orders, 6 drum loads
Compare With a Tolerance That Fits the Data
Sometimes you can’t choose the unit and have to compare two computed floats. Say the order dashboard sends its own total, already added up in kilos, and the script checks it against the month’s 72.0 kg. In Python, you’d write math.isclose(total_kg, 72.0). JavaScript has no counterpart, so you check the difference against a tolerance yourself:
const TOLERANCE_KG = 0.05;
const totalKg = 12.6 + 11.6 + 4.8 + 9.7 + 4.2 + 8.9 + 11.8 + 8.4;
console.log(totalKg);
console.log(Math.abs(totalKg - 72) < TOLERANCE_KG);
The total has the same rounding error as before, yet still counts as 72 kg:
72.00000000000001
true
The tolerance is the roastery’s own rule: totals within half a scale reading, 0.05 kg, count as the same. This tolerance far exceeds any rounding error from adding doubles. By default, math.isclose() uses rel_tol to allow a difference relative to the values’ size, but this rule is a fixed allowance in kilos. In Python, that’s math.isclose(total_kg, 72.0, abs_tol=0.05) or abs(total_kg - 72.0) < 0.05. The JavaScript line ports the latter.
Watch out: Number.EPSILON looks like a ready-made tolerance, but it’s the gap between 1 and the next double, far smaller than the gap between doubles near 72, so Math.abs(totalKg - 72) < Number.EPSILON is false.
So in JavaScript, convert values measured to a fixed precision into a whole unit as they arrive, and back into kilos only for display. Compare computed floats with a tolerance that fits the data. And .toFixed() changes only what you print, while the value the script uses keeps its error.
Next up is Counting Money in Cents, where you’ll port the script’s money lines, which Python keeps in decimal.Decimal, and check the month’s total against the web shop’s to the cent.