Formatting Signs, Units, and Locales

With roundingMode: "halfEven", the port’s Bluebell share reads 12%, as Python’s does. The invoices still need a sign on each café’s change since September, kilos with their unit, and German text for a new café in Aachen. Each becomes an option on an Intl.NumberFormat.

Show the Change With a Sign

Python prints each café’s change with {change:+.0%}: -11%, +12%, and -23%. The + flag becomes signDisplay: "always":

Language: JavaScript
const cafes = [
  { name: "Larder Lane", kg: 36, septemberKg: 40.5 },
  { name: "Bluebell Café", kg: 9, septemberKg: 8 },
  { name: "Northside Kitchen", kg: 27, septemberKg: 35 },
];
const signed = new Intl.NumberFormat("en-US", {
  style: "percent",
  signDisplay: "always",
  roundingMode: "halfEven",
});

for (const cafe of cafes) {
  const change = signed.format(cafe.kg / cafe.septemberKg - 1);
  console.log(`${cafe.name}: ${change} since September`);
}

The lines match Python’s:

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

Become a Member to join the conversation.