The Smoke Detector
  • About
    • About TSD
    • Contact us
  • Art / Design
  • Beauty
  • Fashion
  • Interviews
  • Columns
  • Coding
The Smoke Detector
  • About
    • About TSD
    • Contact us
  • Art / Design
  • Beauty
  • Fashion
  • Interviews
  • Columns
  • Coding
  • Coding

Intl.NumberFormat

  • Ivona Josipovic
  • 24 December 2022

I was working on an eCommerce website recently and had to format quite a few numbers. Some needed to be rounded to two decimals, others to be shown with a currency, and just as I was starting to be overwhelmed with all the styles – Intl.NumberFormat came to the rescue.

Intl.NumberFormat allows us to easily transform any number into a currency that will be displayed according to a specific country/currency style. For example:

let total = 12345.6752;
let formatCurrency = new Intl.NumberFormat(undefined, {style: 'currency', currency: 'EUR' });
const totalEUR = formatCurrency.format(total)
   —> gives us ‘€12,345.68′

Let’s look at it step by step. We declared formatCurrency that creates a new Intl.NumberFormat constructor that takes two arguments: a locale (we pass in undefined as we want to use visitors’ default language) and an object of options. In there we define the style property which is currency, and once we did that we also had to define which currency we want – in this example euro. As a result, we get the rounded number (bye, bye annoying decimals) displayed with the chosen currency.

The options object that we passed as the second argument offers more handy options (like units) so I encourage reading more about it here.
P.S. Note two little details – there is no default currency. If style property is currency then you have to define it yourself. And second – you’ll be given back a string so just keep that in mind so you don’t get lost between type changes.

Share
Tweet
Pin it
Share
Share
Related Topics
  • currency
  • Intl.NumberFormat
  • javascript
  • number formatting
Ivona Josipovic

Ivona Josipovic is a co-founder of The Smoke Detector, writer, fashion stylist, social media manager, curious creature, chocolate lover.

You May Also Like
View Post
  • Fashion

Nordgreen Copenhagen watch review

  • Ivona Josipovic
  • 8 August 2020
View Post
  • Fashion
  • Interviews

Interview with Joanna Bark and Elena Ekström, founders of Bukvy

  • Ivona Josipovic
  • 30 March 2020
View Post
  • Fashion

The evolution of male red carpet style

  • Ivona Josipovic
  • 18 March 2020
View Post
  • Fashion

Christian Louboutin Exhibition, L’Exhibitioniste – Paris 2020

  • Ivona Josipovic
  • 25 February 2020
View Post
  • Fashion

Best Fashion documentaries – part 3

  • Ivona Josipovic
  • 6 March 2020
View Post
  • Fashion

Best handbag brands to know in 2020

  • Ivona Josipovic
  • 11 February 2020

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Featured Posts
  • 1
    • Fashion
    Nordgreen Copenhagen watch review
    • 8 August 2020
  • 2
    • Fashion
    • Interviews
    Interview with Joanna Bark and Elena Ekström, founders of Bukvy
    • 30 March 2020
  • 3
    • Fashion
    The evolution of male red carpet style
    • 18 March 2020
  • 4
    • Fashion
    Christian Louboutin Exhibition, L’Exhibitioniste – Paris 2020
    • 25 February 2020
  • 5
    • Fashion
    Best Fashion documentaries – part 3
    • 6 March 2020
  • 6
    • Fashion
    Best handbag brands to know in 2020
    • 11 February 2020
  • 7
    • Fashion
    Best fashion quotes with strong messages
    • 31 January 2020
  • 8
    • Fashion
    10 ways fashion has changed in the 2010s
    • 4 January 2020
  • 9
    • Art / Design
    • Interviews
    Interview with Elisabeth van Aalderen, the photographer behind Vitiligo photo project
    • 16 December 2019
  • 10
    • Art / Design
    Illustrators celebrating womanhood we love following on IG
    • 10 January 2020
Reflection. 🌴
The world need more love. BLM! 🖤 Regram from @inesabouchard .
Don't miss reading our interview with the founders of the amazing @bukvybag brand. This female-led accessories brand is based in Stockholm and focused on creating practical pieces for everyday life. 😍 Link in bio. #bukvy #bukvybag #interview #femalefounders
Daydreaming of places as this one... 💙 Follow the super talented @salvalopez for more amazing images like this... #photography #pink #daydreaming
Gorgeous images by @nina_zimolong. 💟 In case you missed our interview with the talented photographer go to our site to read it. #photography #interview #love #stunning
Lazy Sunday mani inspo from @vanessahong. Nothing like some cute polka dots for mood boosting, right? 😍 #cute #manicure #loveit #polkadots
The Smoke Detector
  • About
  • Art / Design
  • Beauty
  • Fashion
  • Interviews
  • Columns
  • Coding
The Smoke Detector covers photography, fashion, art, design, cool people and places

Input your search keywords and press Enter.