Created
April 15, 2013 07:19
-
-
Save yogasukma/5386328 to your computer and use it in GitHub Desktop.
this will formated number to curency in javascript
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
| Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator) { | |
| var n = this, | |
| decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces, | |
| decSeparator = decSeparator == undefined ? "." : decSeparator, | |
| thouSeparator = thouSeparator == undefined ? "," : thouSeparator, | |
| sign = n < 0 ? "-" : "", | |
| i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "", | |
| j = (j = i.length) > 3 ? j % 3 : 0; | |
| return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : ""); | |
| }; | |
| var formattedMoney = numberVariable.formatMoney(0,',','.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment