Counting Money in Cents
In this lesson, you ported the money lines of the invoice script. The Python keeps every amount in
a Decimal built from text, rounds each café’s amount with
quantize(Decimal("0.01"), rounding=ROUND_HALF_UP), and checks the month’s total against the web
shop’s with ==. It prints Month total €1,303.20 matches the web shop.
- JavaScript’s everyday number type is the same binary double as Python’s
float, and there’s no decimal type. The line-by-line port writesNumber()forDecimal(), the recipeMath.round(amount * 100) / 100forquantize(), and===for==. - The port prints the three amounts as Python does,
€666.00,€172.80and€464.40, but its check line readsCheck the export: €1,303.20 here, €1303.20 in the shop. Decimal("464.40")keeps exactly the digits you give it. A double stores the nearest binary fraction,464.39999999999997726263, and each+=rounds again, up or down, somonthTotallands a hair below, at1303.1999999999998. The shop’sNumber("1303.20")is a different double, and===compares the doubles themselves, so it givesfalse.- The euro formatter rounds to two decimals for display, so both totals print as
€1,303.20. It hides the error, but the value keeps it. Python’s floats give the same total in a+=loop:Decimalis what made==safe. - The recipe rounds the stored double, so a half cent can go the wrong way:
1.005is stored as1.00499999999999989342, soMath.round(1.005 * 100) / 100is1, not1.01, and(1.005).toFixed(2)is"1.00". - The JavaScript way is to count money in whole cents, as you count weights in grams. Convert the
price text where it comes in with
Math.round(Number(text) * 100):"18.50"becomes1850. - Python’s
int(Decimal(price) * 100)ported withMath.trunc()drops a cent:Number("18.40") * 100is1839.9999999999998, soMath.trunc()gives1839, asint()would on a float, whereMath.round()gives1840. - Each order adds price cents times grams, a whole number of thousandths of a cent: Larder Lane’s
first order is
1850 * 12600,23310000. Where Python callsquantize(), divide by 1000 and round once per café withMath.round(amount / 1000). The sum is whole, so a half cent comes out as exactly.5, and for positive amountsMath.round()sends it up, asROUND_HALF_UPdoes. - The month total is
130320cents, andtoCents("1303.20")is130320too, so===givestrue. Divide by 100 only on the output line. Whole numbers stay exact up to2 ** 53, far past any invoice.
Here are the money lines in cents, run on October’s three cafés and eight orders (the order IDs left out):
const euros = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "EUR",
});
const toCents = (text) => Math.round(Number(text) * 100);
const cafes = [
{ name: "Larder Lane", pricePerKg: "18.50" },
{ name: "Bluebell Café", pricePerKg: "19.20" },
{ name: "Northside Kitchen", pricePerKg: "17.20" },
];
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 monthTotal = 0;
for (const cafe of cafes) {
const priceCents = toCents(cafe.pricePerKg);
let amount = 0;
for (const order of orders) {
if (order.cafe === cafe.name) {
const grams = Math.round(order.kg * 1000);
amount += priceCents * grams;
}
}
amount = Math.round(amount / 1000);
monthTotal += amount;
console.log(`${cafe.name}: ${euros.format(amount / 100)}`);
}
console.log(monthTotal === toCents("1303.20"), monthTotal);
Every amount comes out as Python’s, and the check is exact:
Larder Lane: €666.00
Bluebell Café: €172.80
Northside Kitchen: €464.40
true 130320
Next up is the exercise Check September’s Invoices. There, you’ll port the report’s first lines for September, so they match what the Python script prints.