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

Formatting Euros and Percentages With Intl.NumberFormat

Formatting Euros and Percentages With Intl.NumberFormat

In this lesson, you formatted the batch planner’s green beans and cost lines for the café chain’s 1200 kg order, with thousands separators, euros and percentages, so they print what the Python script prints.

  • In Python, the spec after the colon does the formatting: in ,.2f, the comma asks for thousands separators and .2f for two decimals. JavaScript’s template literals have no format specs, and .toFixed(2) covers only the decimals.
  • JavaScript has a built-in formatter instead, Intl.NumberFormat (Intl is short for internationalization). You build one formatter for each way you print a number, with new, which creates a formatter object and gets a proper treatment later.
  • The first argument names the locale, the language and region whose number conventions to follow. "en-US", US English, uses commas and points, as Python’s , spec does. The second is an object literal of options: style: "currency" with currency: "EUR" formats euros, and adds the euro sign.
  • Watch out: Python’s , spec ignores the locale, but a formatter follows one, so always name it. Leave it out, and JavaScript uses the machine’s: on a server set to German the same total prints as 10.459,06 €.
  • Call .format() on the output line. Like .toFixed(), it returns a string, so the value stays a number until then.
  • style: "percent" multiplies by 100, rounds to a whole percent and adds the sign, as Python’s .0% does, with the same caveat on exact halves as .toFixed(). The last part batch’s share of a drum, 0.6470588235294107, prints as 65%.
  • For ,.1f, set minimumFractionDigits and maximumFractionDigits together, both to 1. The minimum pads with zeros (1,200.0); the maximum rounds off the digits past the last one you keep (1,411.8).
  • The green beans line is long, so it’s split in two. Python joins f-strings that sit side by side, but JavaScript doesn’t join literals, so a + joins the two template literals.
  • For one value printed once, every number has .toLocaleString(), which takes the same locale and options: greenKg.toLocaleString("en-US", { minimumFractionDigits: 1, maximumFractionDigits: 1 }) is 1,411.8, with no formatter to keep.
  • Keep values as numbers through every calculation and format them last. .toFixed() is enough for plain decimals; for anything a person reads, use Intl.NumberFormat with the locale named.

Here are the planner’s two output lines in Python:

Language: Python
print(f"Green beans: {green_kg:,.1f} kg "
      f"(roast loss {WEIGHT_LOSS:.0%})")
print(f"Green coffee: €{total:,.2f} with delivery")

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:02 For the thirty-seven and a half kilo order, the port’s weight and cost match Python’s, with .toFixed() on the output line. Python’s panel shows its full green beans line, roast loss included.

00:15 In this lesson, you’ll add separators, euros and percentages. The café chain’s monthly order is twelve hundred kilos. Python prints both numbers with separators, and the roast loss as a percentage.

00:31 The port runs, but its numbers have no separators, and the roast loss is still missing.

00:39 In Python’s cost line, the spec ,.2f does the work: the comma asks for thousands separators, and .2f for two decimals. JavaScript’s template literals have no format specs, and .toFixed(2) covers only the decimals.

00:57 Instead of a format mini-language, JavaScript has a built-in formatter, Intl.NumberFormat, where Intl is short for internationalization.

01:08 You build one for each way you print a number, and use it on the output line.

01:14 The cost line’s formatter, euros, lands above the output lines. new creates the object; it gets a proper treatment later. The first argument names the locale, the language and region whose conventions to follow: "en-US", US English, with Python’s commas and points.

01:35 The second is an object literal of options: the currency style, with the code "EUR" for euros. Watch out: Python’s spec ignores the locale, but a formatter follows one, so always name it.

01:50 Leave it out, and JavaScript uses the machine’s: a German server prints a point for thousands, a comma for cents, and the euro sign last.

02:01 Now the cost line. The euro sign lifts out of the template literal, because the formatter adds it, and euros.format(total) replaces .toFixed() there.

02:13 total stays a number until this line, and .format() returns a string, as .toFixed() does. Run the port again, and the cost line matches Python’s, separators and euro sign included.

02:28 Python’s .0% spec multiplies by a hundred and adds the percent sign. In JavaScript, that’s a second formatter, percent, whose style is percent.

02:41 The last part batch fills this share of a drum. What will percent.format() print for it? Your turn: pause the video and predict.

02:56 It shows sixty-five percent, as Python’s does. It rounds to a whole percent, with the same caveat on exact halves as .toFixed() had.

03:08 The green beans’ weight needs ,.1f: separators, and exactly one decimal. The third formatter, kilos, sets two options, both to 1.

03:21 minimumFractionDigits pads with zeros, so twelve hundred gets a zero after the point. maximumFractionDigits rounds off the digits past the last one you keep.

03:33 A .1f spec does both at once, so set the two together.

03:39 Now the green beans line. kilos.format() replaces .toFixed() on the weight. The line is long, so, as in Python, the roast loss arrives in a second literal.

03:53 Python joins f-strings that sit side by side, but JavaScript doesn’t, so a + joins the halves. Run the port, and both lines match Python’s.

04:06 For a value you print once, every number also has a .toLocaleString() method. It takes the same locale and options, with no formatter to keep.

04:16 With the options from kilos, the weight comes out the same.

04:22 So keep each value a number through every calculation, and format it last, on the output line. .toFixed() is enough for plain decimals. For anything a person reads, use Intl.NumberFormat, with the locale named.

04:39 Next up is the exercise: Format the Order Summary. A teammate has half ported the planner’s four output lines for the café chain’s twelve-hundred-kilo order, and you’ll make them print exactly what the Python script prints.

Become a Member to join the conversation.