Javascript currency formatting

Hello now I am back again and now I want write how about to convert number to currency format. Maybe we know about javascript plugin to convert currency but it actually has a javascript function itself. Example I from Indonesia and I want currency number to “Rupiah”. If we want use javascript, you must read from this documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat.
Example this is basic number formatting:

var number = 3500;

document.write(new Intl.NumberFormat().format(number));
// Result = 3,500

And this is if I using locales, example I use Indonesian:

var number = 123456.789;

// Indonesian uses comma as decimal separator and period for thousands
document.write(new Intl.NumberFormat('ID').format(number));
// Result = 123.456,789

And for more info list code locales, you can read from this http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
And now how if I want use currency code like ¥ or €

var number = 123456.789;

// the Japanese yen doesn't use a minor unit
document.write(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// Result = ¥123,457

Form more info about style currency code, you can read from this http://www.currency-iso.org/en/home/tables/table-a1.html
For more info about date format you can read on this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s