Created
December 19, 2014 03:00
-
-
Save Tagliatti/5bfdd590f7903f16f701 to your computer and use it in GitHub Desktop.
Number Format
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
function formatNumber(number) | |
{ | |
number = parseFloat(number); | |
var number = number.toFixed(2) + ''; | |
var x = number.split('.'); | |
var decimal = x[0]; | |
var value = x.length > 1 ? ',' + x[1] : ''; | |
var pattern = /(\d+)(\d{3})/; | |
while (pattern.test(decimal)) { | |
decimal = decimal.replace(pattern, '$1' + '.' + '$2'); | |
} | |
return decimal + value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment