Skip to content

Instantly share code, notes, and snippets.

@fcgomes92
Created February 4, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcgomes92/2899550a2b4a86b2f0d77b49c04513ad to your computer and use it in GitHub Desktop.
Save fcgomes92/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