Created
March 27, 2012 06:45
-
-
Save ptquang86/2213434 to your computer and use it in GitHub Desktop.
Javascript - Format number
This file contains hidden or 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
exports.digits = function(number){ | |
if (!isNaN(number)) { | |
number += ''; | |
} | |
if (number == '') { | |
return 0; | |
} | |
number = exports.replaceAll(number, ',', ''); | |
number = parseFloat(number).toFixed(2); | |
return number.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); | |
}; | |
//replace all match char in string | |
exports.replaceAll = function(source, pattern, replacement){ | |
return source.split(pattern).join(replacement); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment