Format currency with Intl

The Intl.NumberFormat() object enables language-sensitive number formatting. We can use this to convert currency, numbers, percentages, units, and other notation into formatted strings. This is particularly useful in eCommerce applications, with examples like displaying an item price or recipt printing. Another example are social platforms like YouTube, which use compact notation to display high counts of views and followers.

Currency formatting

📚 You can reference currency codes here and locale values here

Number to $USD

file_type_js_official showMeTheMoney.js
const convertToUSD = (someNumber) => {
  return new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
  }).format(someNumber);
};

// Get dollas in english 🇺🇸
convertToUSD(12.99); // "$12.99"

Number to EURO €

file_type_js_official zeigMirDasGeld.js
const inEuroUmrechnen = (eineZahl) => {
  return new Intl.NumberFormat("de-DE", {
    style: "currency",
    currency: "EUR",
  }).format(eineZahl);
};

// Holen sie sich Euro in deutsche 🇩🇪
inEuroUmrechnen(39.99); // "39,99 €"

Compact notation

file_type_js_official oneMillionSubs.js
const subscriberCount = (subscribers) => {
  return new Intl.NumberFormat("en-US", {
    notation: "compact",
  compactDisplay: "short"
  }).format(subscribers);
};

// 📼 Current Fireship subscribers 
subscriberCount(711000); // "711K"

// 🔥 Fireship subscriber goal for 2021
subscriberCount(1000000); // "1M"

Unit conversion

file_type_js_official unitConversion.js
const convertToGigabytes = (megabytes) => {
  return new Intl.NumberFormat("en-US", {
    style: "unit",
    unit: "gigabyte",
    maximumSignificantDigits: 4,
    unitDisplay: "narrow"
  }).format(megabytes / 1000);
};

// 💾 Convert MB value to GB:
convertToGigabytes(77778); // "77.78GB"

🔥 BONUS EXAMPLE 🔥

file_type_js_official getStorageBill.js
// Example monthly Firebase storage used:
const stored = convertToGigabytes(77778); // "77.78GB"

// 5GB Free tier monthly allowance:
const free = 5;

// Calculate total Firebase storage bill:
const billable = (parseFloat(stored) - free); // 72.78

// Format total using currency conversion
const totalBill = convertToUSD(billable * 0.026); // "$1.89"

Temperature conversion

file_type_js_official tempConversion.js
// 🌡 Convert tempertaure to °C or °F:
const convertTemp = (unit, degrees) => {

  // Handle output based on unit input:
  switch (unit) {
    case "celsius":
      degrees = ((degrees - 32) / 1.8000);
      break;
    case "fahrenheit":
      degrees = ((degrees * 1.8000) + 32);
      break;
  }

  return new Intl.NumberFormat("en-US", {
    style: "unit",
    unit: unit,
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(degrees);
};

// °C → 🥶 → °F
convertTemp("fahrenheit", 21); // "69.80°F"

// °F → 🔥 → °C
convertTemp("celsius", 97); // "36.11° C"

You can learn more about Intl.NumberFormat(), the Internationalization API, and the ECMAScript specification for all the properties, values, and methods available on MDN

Q&A Chat