Skip to content

Instantly share code, notes, and snippets.

@zikes
Created January 28, 2011 16:29
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 zikes/800504 to your computer and use it in GitHub Desktop.
Save zikes/800504 to your computer and use it in GitHub Desktop.
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