Skip to content

Instantly share code, notes, and snippets.

@smokinggoats
Created February 4, 2020 18:38
Show Gist options
  • Save smokinggoats/2899550a2b4a86b2f0d77b49c04513ad to your computer and use it in GitHub Desktop.
Save smokinggoats/2899550a2b4a86b2f0d77b49c04513ad to your computer and use it in GitHub Desktop.
Simple currency formatter
function formatCurrency(number) {
const [integerPart, decimalPart] = `${number}`.split(".");
const chunkSize = 3;
const integerPartArray = integerPart.split("").reverse();
const chunks = Math.ceil(integerPartArray.length / chunkSize);
const formatedInteger = Array.from(new Array(chunks), (_, idx) => {
return integerPartArray
.slice(idx * chunkSize, idx * chunkSize + chunkSize)
.reverse()
.join("");
}).reverse();
return `${formatedInteger.join(",")}.${decimalPart}`;
}
// TODO: document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment