Internationalize Numbers with JavaScript


Presenting numbers in a readable structure takes many bureaucracy, from visible charts to easily including punctuation. The ones punctuation, on the other hand, are other in accordance with internationalization. Some international locations use , for decimal, whilst others use .. Frightened about having to code for all this insanity? Do not — JavaScript supplies a technique do the laborious give you the results you want!

The Quantity primitive has a toLocaleString option to do the elemental formatting for you:

const value = 16601.91;

// Fundamental decimal structure, no offering locale
// Makes use of locale supplied through browser since none outlined
value.toLocaleString(); // "16,601.91"

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

// Formatting forex is conceivable
value.toLocaleString('de-DE', { 
  taste: 'forex', 
  forex: 'EUR' 
}); // "16.601,91 €"

// You'll be able to additionally use Intl.NumberFormat for formatting
new Intl.NumberFormat('en-US', {
  taste: 'forex',
  forex: 'GBP'
}).structure(value); // £16,601.91

It is a main reduction that JavaScript supplies us those form of helpers in order that we do not wish to depend on bloated third-party libraries. No excuses — the software is there!


Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: