Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript Currency Formatter
function fmt_currency(num){
var str=parseFloat(num).toFixed(2);
var arr=str.split('');
var out='';
var char='';
for(var x=0, len=arr.length;x<len;x++){
if(x<=2){
out=arr.pop()+out;
}else{
char=arr.pop();
if((x-2)%3==1 && x!==3 && !isNaN(parseInt(char,10))){out=','+out;}
out=char+out;
}
}
return '$'+out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment