Skip to content

Instantly share code, notes, and snippets.

@nikhresna
Last active September 28, 2016 18:07
Show Gist options
  • Save nikhresna/855a6030bd2d2f321c4f5122a1bd4517 to your computer and use it in GitHub Desktop.
Save nikhresna/855a6030bd2d2f321c4f5122a1bd4517 to your computer and use it in GitHub Desktop.
Javascript currency formatter
function format(num) {
let arr = num.toString().split('').reverse();
let j = 0;
let result = '';
for(let i = 0; i < arr.length; i++) {
j = i % 3;
result = arr[i] + result;
if(j == 2 && (i < arr.length - 1)) {
result = '.' + result;
}
}
return 'Rp. ' + result;
}
format(100); // returns 'Rp. 100'
format(1000); // returns 'Rp. 1.000'
format(10000); // returns 'Rp. 10.000'
format(100000); // returns 'Rp. 100.000'
format(1000000); // returns 'Rp. 1.000.000'
// credit: kuntoaji https://github.com/kuntoaji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment