format currency in jquery

So when we are in web development we want show Currency value with some formatting. Like thousand separator, decimal separator and currency symbol.

I am using below Jquery function to format the currency values.


function formatCurrency(value, decimals, decpoint, separator, symbol) {
decimals = (typeof decimals == 'undefined' ? 2 : decimals);
decpoint = (typeof decpoint == 'undefined' ? '.' : decpoint);
separator = (typeof separator == 'undefined' ? ',' : separator);
symbol = (typeof symbol == 'undefined' ? '$' : symbol);

var parts = value.toFixed(decimals).toString().split(decpoint);
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, separator);

return (symbol + parts.join(decpoint)).toLocaleString();
}

We can format currency value as we required. If we just pass the currency value it will give us value with default formatting.

formatCurrency(5873839) will give us ” $5,873,839.00″

Advertisements

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