Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Format currency with JavaScript
// Takes a Number and returns a US/CAN currency string.
function toCurrency(amount) {
return "$" + amount.toFixed(2);
};
// Adds a .toCurrency() method to Numbers.
Number.prototype.toCurrency = function() {
return toCurrency(this);
};
console.log( (15.4).toCurrency() ); // "$15.40"
@osrec

This comment has been minimized.

Copy link

osrec commented Jun 20, 2018

This works, but if you need a more complete solution, try currencyFornatter.js:

https://osrec.github.io/currencyFormatter.js/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.