Skip to content

Instantly share code, notes, and snippets.

@ptquang86
Created March 27, 2012 06:45
Show Gist options
  • Save ptquang86/2213434 to your computer and use it in GitHub Desktop.
Save ptquang86/2213434 to your computer and use it in GitHub Desktop.
Javascript - Format number
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