<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Wojciech Grabowski</title>
    <description>The latest articles on DEV Community by Wojciech Grabowski (@wojtek).</description>
    <link>https://dev.to/wojtek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35402%2F58b4d9c8-6113-4a94-a804-65073cd900fd.jpg</url>
      <title>DEV Community: Wojciech Grabowski</title>
      <link>https://dev.to/wojtek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wojtek"/>
    <language>en</language>
    <item>
      <title>How to format currency with Intl.NumberFormat</title>
      <dc:creator>Wojciech Grabowski</dc:creator>
      <pubDate>Fri, 07 Jul 2023 16:51:33 +0000</pubDate>
      <link>https://dev.to/wojtek/how-to-format-currency-with-intlnumberformat-4eia</link>
      <guid>https://dev.to/wojtek/how-to-format-currency-with-intlnumberformat-4eia</guid>
      <description>&lt;p&gt;Displaying currency formatted depending on user language/region is not uncommon task in web development.&lt;/p&gt;

&lt;p&gt;It can be easily achieved with usage of &lt;code&gt;Intl&lt;/code&gt;, which is a namespace in browser API and part of &lt;em&gt;ECMAScript Internationalization API&lt;/em&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Intl namespace object contains several constructors as well as functionality common to the internationalization constructors and other language sensitive functions. Collectively, they comprise the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, date and time formatting, and more. &lt;cite&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl"&gt;MDN Intl page&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;This is ready to use code that display &lt;code&gt;amount&lt;/code&gt; of &lt;code&gt;currency&lt;/code&gt; formatted for given &lt;code&gt;locale&lt;/code&gt;. These data are usually taken from API or application state or in case of locale from browser API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function formatCurrency(locale,amount,currency){
const formatter = new Intl.NumberFormat(locale,
 {
   style:'currency',
   currency: currency
 });

 return formatter.format(amount);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is all that you need. Simple function that can be used in any framework, no npm packages needed, no dictionaries with currency symbols etc.&lt;br&gt;
There other options that can be set to customize how currency is presented, all of them well described &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#parameters"&gt;on MDN as well&lt;/a&gt;. Interactive examples are there as well so I will not rewrite them here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let the browser worry
&lt;/h2&gt;

&lt;p&gt;Even if your application is not localized or uses only single currency it is convenient to let the browser worry about how currency should be displayed: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what is currency symbol ?&lt;/li&gt;
&lt;li&gt;should symbol be placed before or after amount?&lt;/li&gt;
&lt;li&gt;use spaces or comma for separating thousands and decimal separator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other thing is that using standardized formatting assures that screen readers and other assistive technologies will read that data correctly.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>intl</category>
      <category>i18n</category>
    </item>
  </channel>
</rss>
