Skip to content

Instantly share code, notes, and snippets.

@hotoo
Created December 20, 2010 10:49
Show Gist options
  • Save hotoo/748249 to your computer and use it in GitHub Desktop.
Save hotoo/748249 to your computer and use it in GitHub Desktop.
function commfy0(n){
var a = n.toString().split(".");
var i = a[0].length%3;
var prefix = (a[0].substr(0,i)+a[0].substr(i).replace(/(\d{3})/g,",$1")).replace(/^,/, "");
//var postfix = a[1] ? "."+a[1].replace(/(\d{3})/g, "$1,").replace(/,$/, "") : "";
return prefix+(a[1]?"."+a[1]:"");
}
var commfy1 = (function(){
var re=/(\d{1,3})(?=(?:\d{3})+(?:$|\.))/g;
return function(n){
var a=n.toString().split(".");
a[0] = a[0].replace(re,"$1,");
return a.join(".");
};
})();
var commfy2 = (function(){
var re=/\B(?=(?:\d{3})+$)/g;
return function(n){
var a=n.toString().split(".");
a[0] = a[0].replace(re,",");
return a.join(".");
};
})();
@hotoo
Copy link
Author

hotoo commented Dec 20, 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment