Software Development

Find out how to Internationalize Numbers with JavaScript

Spread the love

Presenting numbers in a readable format takes many kinds, from visible charts to easily including punctuation. These punctuation, nonetheless, are completely different primarily based on internationalization. Some international locations use , for decimal, whereas others use .. Fearful about having to code for all this insanity? Do not — JavaScript offers a technique do the laborious give you the results you want!

The Quantity primitive has a toLocaleString methodology to do the fundamental formatting for you:

const value = 16601.91;

// Primary decimal format, no offering locale
// Makes use of locale offered by browser since none outlined
value.toLocaleString(); // "16,601.91"

// Present a particular locale
value.toLocaleString('de-DE'); // "16.601,91"

// Formatting foreign money is feasible
value.toLocaleString('de-DE', { 
  type: 'foreign money', 
  foreign money: 'EUR' 
}); // "16.601,91 €"

// You can even use Intl.NumberFormat for formatting
new Intl.NumberFormat('en-US', {
  type: 'foreign money',
  foreign money: 'GBP'
}).format(value); // £16,601.91

It is a main aid that JavaScript offers us these sort of helpers in order that we need not depend on bloated third-party libraries. No excuses — the instrument is there!

  • Use Custom Missing Image Graphics Using MooTools

    Lacking photos in your web site could make you or your small business look fully newbie. Sadly generally a picture will get deleted or corrupted with out your information. You’d agree with me that IE’s default “purple x” icon seems terrible, so why not use your personal lacking picture graphic? The MooTools JavaScript Word that…

  • Save Text Size Preference Using MooTools 1.2

Leave a Reply

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