Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Matching Python's Rounding With roundingMode

Matching Python’s Rounding With roundingMode

In this lesson, you ported the share on each café’s invoice line. The Python prints it with the .0% format spec, {share:.0%}, and the line reads Bluebell Café: 9.0 kg (12%), €172.80, RF3540422202610.

  • The percent formatter, an Intl.NumberFormat with style: "percent", multiplies by 100, adds the percent sign, and rounds to a whole percent. Ported with it, Larder Lane’s 50% and Northside’s 38% match, but Bluebell’s line reads 13%, with no error.
  • Bluebell’s share, 9 / 72, is exactly one eighth, and a double stores it exactly, so the share is 12.5% on the dot: a half, equally close to 12 and 13.
  • Python’s format specs round an exact half to the even digit, as round() does: 12%. The default rounding mode of Intl.NumberFormat, "halfExpand", rounds a half away from zero: 13%. Northside’s 37.5% gives 38% either way, because 38 is even.
  • Add roundingMode: "halfEven" to the formatter’s options, and Bluebell’s line shows 12%, as Python’s does. roundingMode is the formatter’s version of rounding= in quantize(), with the opposite default: without rounding=, quantize() rounds a half to even. Five eighths, 0.625, is 63% by default and 62% with "halfEven", as in Python.
  • The euro amounts need no change: whole cents never land on half a cent at two decimals.
  • The other modes include "trunc" (ROUND_DOWN), "ceil" (ROUND_CEILING) and "floor" (ROUND_FLOOR). "trunc" and "floor" also give 12% for Bluebell, but 37% for Northside. "halfEven" is the one that rounds each exact half as Python does.
  • .toFixed() rounds halves away from zero, like the formatter’s default: (12.5).toFixed(0) is "13". It has no option to change that, which is one more reason to format with Intl.NumberFormat.
  • One limit: "halfEven" matches Python on exact halves. Both languages print 0.545 as 0.545, but the double is a hair above the half. Python’s .0% rounds the stored value, 55%, while the formatter rounds the printed 0.545, an exact half to it, 54%.
  • When a port must print what the Python printed, put the rounding rule in the formatter, once. Every call then rounds exact halves as Python does; for values near a half, like 0.545, compare with Python’s output.

Here’s the share formatter on October’s three cafés, in grams:

Language: JavaScript
const MONTH_GRAMS = 72000;
const cafeTotals = [
  { name: "Larder Lane", grams: 36000 },
  { name: "Bluebell Café", grams: 9000 },
  { name: "Northside Kitchen", grams: 27000 },
];

const percent = new Intl.NumberFormat("en-US", {
  style: "percent",
  roundingMode: "halfEven",
});

for (const cafe of cafeTotals) {
  const share = cafe.grams / MONTH_GRAMS;
  console.log(`${cafe.name}: ${percent.format(share)}`);
}
console.log(percent.format(0.625));

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The port’s invoice lines have to match the Python’s, character for character. In this lesson, you’ll port the share on each café’s line. The Python divides the café’s kilos by the month’s 72.

00:13 The .0% spec turns that share into a whole percent. The other parts of the line, the kilos, the amount and the reference, come from the earlier lessons.

00:26 Python prints three invoice lines: 50, 12 and 38 percent.

00:34 In the JavaScript port, the .0% spec becomes a percent formatter. It’s an Intl.NumberFormat whose style is "percent": it multiplies by 100, adds the percent sign, and rounds to a whole percent.

00:48 The port keeps each café’s weight in grams, so for each café, the share divides grams by the month’s grams. The kilos and the amount carry over, and the share goes through the formatter.

01:01 Run it. Larder Lane’s 50 and Northside’s 38 match Python’s. Bluebell’s share is exactly 0.125. What share does Bluebell’s line show? Your turn: pause the video and predict.

01:23 It shows 13 percent. Python printed 12, and JavaScript gave no error, no warning, only a different invoice.

01:34 The share isn’t a float error. Nine over 72 is exactly one eighth, and a double stores an eighth exactly. So Bluebell’s share is 12.5 percent on the dot, a half, equally close to 12 and 13.

01:51 Python’s format specs round an exact half to the even digit, as round() does: 12. The formatter rounds by its roundingMode, and the default, halfExpand, rounds a half away from zero: 13.

02:05 Northside’s 37.5 is a half too. But 38 is even and away from zero, so both land on 38.

02:17 The fix is one option. Tell the formatter the rule: roundingMode, set to halfEven, rounds an exact half to the even neighbor, as Python does.

02:28 It’s the formatter’s version of the rounding= argument in quantize(), with the opposite default: leave it out, and quantize() rounds a half to even.

02:37 Run it again, and Bluebell’s line shows 12 percent. All three lines match Python’s. The amounts matched all along: they’re whole cents, which never land on half a cent at two decimals, so the euro formatter keeps its default.

02:55 Try another exact half: five eighths. The default gives 63 percent. With halfEven, it’s 62, and so is Python’s.

03:07 roundingMode takes other rules too. halfExpand is the default. trunc cuts the extra digits off, and ceil and floor round up and down.

03:18 In Decimal’s names, watch ROUND_DOWN: it’s trunc, not floor, in JavaScript. trunc and floor give 12 for Bluebell too, but they cut Northside’s 37.5 to 37.

03:32 Only halfEven rounds each half the way Python does. As you’ve seen, .toFixed() rounds halves away from zero, like the default, and has no option to change that, one more reason to let Intl.NumberFormat do the formatting.

03:48 One limit. halfEven matches Python on exact halves like these eighths. Both languages print 0.545 as 0.545, but the double is a hair above the half.

04:02 Python’s spec rounds the stored value: 55. The formatter rounds the printed 0.545, an exact half to it: 54.

04:15 Python rounds an exact half to even, and Intl.NumberFormat rounds it away from zero unless you say otherwise. When a port must print what the Python printed, put the rule in the formatter, once.

04:28 Every call to its .format() method follows it. Still, for values near a half, like 0.545, compare with Python’s output.

04:40 Next up is Formatting Signs, Units, and Locales, a text lesson. There, you’ll give each café’s change since September its sign, show kilos with their unit, and write invoices in German for a new café.

Become a Member to join the conversation.