Hemanth's Scribes

javascript

Handling Currency in JavaScript

Author Photo

Hemanth HM

Thumbnail

Handling currency got better with the Intl object, which provides a namespace for standard internationalization constructors.

Intl Constructors

  • Collator: Language sensitive string comparison
  • DateTimeFormat: Language sensitive date and time formatting
  • NumberFormat: Language sensitive number formatting

Currency Formatting

var currency = function(type) {
  return new Intl.NumberFormat(["en-IN"], {
    style: "currency",
    currency: type,
    currencyDisplay: "symbol",
    maximumFractionDigits: 1
  });
};

## Examples
javascript
currency("INR").format(10023.56)
// "Rs10,023.56"

currency("USD").format(10023.56)
// "$10,023.56"

currency("GBP").format(10023.56)
// "£10,023.56"

currency("CNY").format(10023.56)
// "CN¥10,023.56"

Hope there shall be a standard API for currency conversion too!

#javascript#intl
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.