Created
December 20, 2010 10:49
-
-
Save hotoo/748249 to your computer and use it in GitHub Desktop.
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 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]:""); | |
} |
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
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("."); | |
}; | |
})(); |
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
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("."); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://blog.hotoo.me/javascript-commfy.html