Last active
September 28, 2016 18:07
-
-
Save nikhresna/855a6030bd2d2f321c4f5122a1bd4517 to your computer and use it in GitHub Desktop.
Javascript 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 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