Created
February 4, 2020 18:38
-
-
Save smokinggoats/2899550a2b4a86b2f0d77b49c04513ad to your computer and use it in GitHub Desktop.
Simple currency formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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